Skip to content

Instantly share code, notes, and snippets.

@c9s
Created February 5, 2017 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c9s/c50072603d309b31767bc43aaa80f9ee to your computer and use it in GitHub Desktop.
Save c9s/c50072603d309b31767bc43aaa80f9ee to your computer and use it in GitHub Desktop.
<?php
namespace AuthorBooks\Model;
require_once __DIR__ . '/AuthorSchemaProxy.php';
use Maghead\Schema\SchemaLoader;
use Maghead\Result;
use Maghead\Inflator;
use SQLBuilder\Bind;
use SQLBuilder\ArgumentArray;
use SQLBuilder\Universal\Query\InsertQuery;
use SQLBuilder\Driver\BaseDriver;
use SQLBuilder\Driver\PDOMySQLDriver;
use PDO;
use DateTime;
use Maghead\Runtime\BaseModel;
class AuthorBase
extends BaseModel
{
const SCHEMA_CLASS = 'AuthorBooks\\Model\\AuthorSchema';
const SCHEMA_PROXY_CLASS = 'AuthorBooks\\Model\\AuthorSchemaProxy';
const COLLECTION_CLASS = 'AuthorBooks\\Model\\AuthorCollection';
const MODEL_CLASS = 'AuthorBooks\\Model\\Author';
const TABLE = 'authors';
const READ_SOURCE_ID = 'default';
const WRITE_SOURCE_ID = 'default';
const PRIMARY_KEY = 'id';
const TABLE_ALIAS = 'm';
public static $column_names = array (
0 => 'id',
1 => 'name',
2 => 'email',
3 => 'identity',
4 => 'confirmed',
5 => 'created_on',
6 => 'updated_on',
);
public static $mixin_classes = array (
0 => 'Maghead\\Schema\\Mixin\\MetadataMixinSchema',
);
protected $table = 'authors';
public $readSourceId = 'default';
public $writeSourceId = 'default';
public $id;
public $name;
public $email;
public $identity;
public $confirmed;
public $created_on;
public $updated_on;
public static function getSchema()
{
static $schema;
if ($schema) {
return $schema;
}
return $schema = new \AuthorBooks\Model\AuthorSchemaProxy;
}
public static function createRepo($write, $read)
{
return new \AuthorBooks\Model\AuthorBaseRepo($write, $read);
}
public function getId()
{
return intval($this->id);
}
public function getName()
{
return $this->name;
}
public function loadByName($value)
{
return static::masterRepo()->loadByName($value);
}
public function getEmail()
{
return $this->email;
}
public function loadByEmail($value)
{
return static::masterRepo()->loadByEmail($value);
}
public function getIdentity()
{
return $this->identity;
}
public function loadByIdentity($value)
{
return static::masterRepo()->loadByIdentity($value);
}
public function isConfirmed()
{
$value = $this->confirmed;
if ($value === '' || $value === null) {
return null;
}
return boolval($value);
}
public function getCreatedOn()
{
return Inflator::inflate($this->created_on, 'DateTime');
}
public function getUpdatedOn()
{
return Inflator::inflate($this->updated_on, 'DateTime');
}
public function getKeyName()
{
return 'id';
}
public function getKey()
{
return $this->id;
}
public function hasKey()
{
return isset($this->id);
}
public function setKey($key)
{
return $this->id = $key;
}
public function getData()
{
return ["id" => $this->id, "name" => $this->name, "email" => $this->email, "identity" => $this->identity, "confirmed" => $this->confirmed, "created_on" => $this->created_on, "updated_on" => $this->updated_on];
}
public function setData(array $data)
{
if (array_key_exists("id", $data)) { $this->id = $data["id"]; }
if (array_key_exists("name", $data)) { $this->name = $data["name"]; }
if (array_key_exists("email", $data)) { $this->email = $data["email"]; }
if (array_key_exists("identity", $data)) { $this->identity = $data["identity"]; }
if (array_key_exists("confirmed", $data)) { $this->confirmed = $data["confirmed"]; }
if (array_key_exists("created_on", $data)) { $this->created_on = $data["created_on"]; }
if (array_key_exists("updated_on", $data)) { $this->updated_on = $data["updated_on"]; }
}
public function clear()
{
$this->id = NULL;
$this->name = NULL;
$this->email = NULL;
$this->identity = NULL;
$this->confirmed = NULL;
$this->created_on = NULL;
$this->updated_on = NULL;
}
public function fetchAddresses()
{
return static::masterRepo()->fetchAddressesOf($this);
}
public function getAddresses()
{
$collection = new \AuthorBooks\Model\AddressCollection;
$collection->where()->equal("author_id", $this->id);
$collection->setPresetVars([ "author_id" => $this->id ]);
return $collection;
}
public function fetchUnusedAddresses()
{
return static::masterRepo()->fetchUnusedAddressesOf($this);
}
public function getUnusedAddresses()
{
$collection = new \AuthorBooks\Model\AddressCollection;
$collection->where()->equal("author_id", $this->id);
$collection->setPresetVars([ "author_id" => $this->id ]);
return $collection;
}
public function fetchAuthorBooks()
{
return static::masterRepo()->fetchAuthorBooksOf($this);
}
public function getAuthorBooks()
{
$collection = new \AuthorBooks\Model\AuthorBookCollection;
$collection->where()->equal("author_id", $this->id);
$collection->setPresetVars([ "author_id" => $this->id ]);
return $collection;
}
public function getBooks()
{
$collection = new \AuthorBooks\Model\BookCollection;
$collection->joinTable('author_books', 'j', 'INNER')
->on("j.book_id = {$collection->getAlias()}.id");
$collection->where()->equal('j.author_id', $this->id);
$parent = $this;
$collection->setAfterCreate(function($record, $args) use ($parent) {
$a = [
'book_id' => $record->get("id"),
'author_id' => $parent->id,
];
if (isset($args['author_books'])) {
$a = array_merge($args['author_books'], $a);
}
return \AuthorBooks\Model\AuthorBook::createAndLoad($a);
});
return $collection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment