Instantly share code, notes, and snippets.
Created
January 25, 2017 04:20
-
Save c9s/27dc4c802a02c6640be1037d22fd7d8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AuthorBooks\Model; | |
require_once __DIR__ . '/AuthorSchemaProxy.php'; | |
use Maghead\Schema\SchemaLoader; | |
use Maghead\Result; | |
use Maghead\BaseModel; | |
use Maghead\Inflator; | |
use SQLBuilder\Bind; | |
use SQLBuilder\ArgumentArray; | |
use PDO; | |
use SQLBuilder\Universal\Query\InsertQuery; | |
use Maghead\BaseRepo; | |
class AuthorBaseRepo | |
extends BaseRepo | |
{ | |
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'; | |
const FIND_BY_PRIMARY_KEY_SQL = 'SELECT * FROM authors WHERE id = ? LIMIT 1'; | |
const LOAD_BY_NAME_SQL = 'SELECT * FROM authors WHERE name = :name LIMIT 1'; | |
const LOAD_BY_EMAIL_SQL = 'SELECT * FROM authors WHERE email = :email LIMIT 1'; | |
const LOAD_BY_IDENTITY_SQL = 'SELECT * FROM authors WHERE identity = :identity LIMIT 1'; | |
const DELETE_BY_PRIMARY_KEY_SQL = 'DELETE FROM authors WHERE id = ?'; | |
const FETCH_ADDRESSES_SQL = 'SELECT * FROM addresses WHERE author_id = ?'; | |
const FETCH_UNUSED_ADDRESSES_SQL = 'SELECT * FROM addresses WHERE author_id = ?'; | |
const FETCH_AUTHOR_BOOKS_SQL = 'SELECT * FROM author_books WHERE author_id = ?'; | |
public static $columnNames = array ( | |
0 => 'id', | |
1 => 'name', | |
2 => 'email', | |
3 => 'identity', | |
4 => 'confirmed', | |
5 => 'created_on', | |
6 => 'updated_on', | |
); | |
public static $columnHash = array ( | |
'id' => 1, | |
'name' => 1, | |
'email' => 1, | |
'identity' => 1, | |
'confirmed' => 1, | |
'created_on' => 1, | |
'updated_on' => 1, | |
); | |
public static $mixinClasses = array ( | |
0 => 'Maghead\\Schema\\Mixin\\MetadataMixinSchema', | |
); | |
protected $table = 'authors'; | |
protected $loadStm; | |
protected $deleteStm; | |
protected $loadByNameStm; | |
protected $loadByEmailStm; | |
protected $loadByIdentityStm; | |
protected $fetchAddressesStm; | |
protected $fetchUnusedAddressesStm; | |
protected $fetchAuthorBooksStm; | |
public static function getSchema() | |
{ | |
static $schema; | |
if ($schema) { | |
return $schema; | |
} | |
return $schema = new \AuthorBooks\Model\AuthorSchemaProxy; | |
} | |
public function loadByPrimaryKey($pkId) | |
{ | |
if (!$this->loadStm) { | |
$this->loadStm = $this->read->prepare(self::FIND_BY_PRIMARY_KEY_SQL); | |
$this->loadStm->setFetchMode(PDO::FETCH_CLASS, 'AuthorBooks\Model\Author'); | |
} | |
return static::_stmFetchOne($this->loadStm, [$pkId]); | |
} | |
public function loadByName($value) | |
{ | |
if (!$this->loadByNameStm) { | |
$this->loadByNameStm = $this->read->prepare(self::LOAD_BY_NAME_SQL); | |
$this->loadByNameStm->setFetchMode(PDO::FETCH_CLASS, '\AuthorBooks\Model\Author'); | |
} | |
return static::_stmFetchOne($this->loadByNameStm, [':name' => $value ]); | |
} | |
public function loadByEmail($value) | |
{ | |
if (!$this->loadByEmailStm) { | |
$this->loadByEmailStm = $this->read->prepare(self::LOAD_BY_EMAIL_SQL); | |
$this->loadByEmailStm->setFetchMode(PDO::FETCH_CLASS, '\AuthorBooks\Model\Author'); | |
} | |
return static::_stmFetchOne($this->loadByEmailStm, [':email' => $value ]); | |
} | |
public function loadByIdentity($value) | |
{ | |
if (!$this->loadByIdentityStm) { | |
$this->loadByIdentityStm = $this->read->prepare(self::LOAD_BY_IDENTITY_SQL); | |
$this->loadByIdentityStm->setFetchMode(PDO::FETCH_CLASS, '\AuthorBooks\Model\Author'); | |
} | |
return static::_stmFetchOne($this->loadByIdentityStm, [':identity' => $value ]); | |
} | |
public function deleteByPrimaryKey($pkId) | |
{ | |
if (!$this->deleteStm) { | |
$this->deleteStm = $this->write->prepare(self::DELETE_BY_PRIMARY_KEY_SQL); | |
} | |
return $this->deleteStm->execute([$pkId]); | |
} | |
public function fetchAddressesOf(BaseModel $record) | |
{ | |
if (!$this->fetchAddressesStm) { | |
$this->fetchAddressesStm = $this->read->prepare(self::FETCH_ADDRESSES_SQL); | |
$this->fetchAddressesStm->setFetchMode(PDO::FETCH_CLASS, '\AuthorBooks\Model\Address'); | |
} | |
return static::_stmFetchAll($this->fetchAddressesStm, [$record->id]); | |
} | |
public function fetchUnusedAddressesOf(BaseModel $record) | |
{ | |
if (!$this->fetchUnusedAddressesStm) { | |
$this->fetchUnusedAddressesStm = $this->read->prepare(self::FETCH_UNUSED_ADDRESSES_SQL); | |
$this->fetchUnusedAddressesStm->setFetchMode(PDO::FETCH_CLASS, '\AuthorBooks\Model\Address'); | |
} | |
return static::_stmFetchAll($this->fetchUnusedAddressesStm, [$record->id]); | |
} | |
public function fetchAuthorBooksOf(BaseModel $record) | |
{ | |
if (!$this->fetchAuthorBooksStm) { | |
$this->fetchAuthorBooksStm = $this->read->prepare(self::FETCH_AUTHOR_BOOKS_SQL); | |
$this->fetchAuthorBooksStm->setFetchMode(PDO::FETCH_CLASS, '\AuthorBooks\Model\AuthorBook'); | |
} | |
return static::_stmFetchAll($this->fetchAuthorBooksStm, [$record->id]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment