Last active
January 18, 2017 05:48
-
-
Save c9s/06b4033714fd7f98618e12035a8345f7 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; | |
use LazyRecord\Schema\SchemaLoader; | |
use LazyRecord\Result; | |
use LazyRecord\Inflator; | |
use SQLBuilder\Bind; | |
use SQLBuilder\ArgumentArray; | |
use PDO; | |
use SQLBuilder\Universal\Query\InsertQuery; | |
use TestApp\ModelTrait\EBookTrait; | |
use LazyRecord\BaseModel; | |
use TestApp\ModelInterface\EBookInterface; | |
class BookBase | |
extends BaseModel | |
implements EBookInterface | |
{ | |
use EBookTrait; | |
const SCHEMA_CLASS = 'AuthorBooks\\Model\\BookSchema'; | |
const SCHEMA_PROXY_CLASS = 'AuthorBooks\\Model\\BookSchemaProxy'; | |
const COLLECTION_CLASS = 'AuthorBooks\\Model\\BookCollection'; | |
const MODEL_CLASS = 'AuthorBooks\\Model\\Book'; | |
const TABLE = 'books'; | |
const READ_SOURCE_ID = 'default'; | |
const WRITE_SOURCE_ID = 'default'; | |
const PRIMARY_KEY = 'id'; | |
const FIND_BY_PRIMARY_KEY_SQL = 'SELECT * FROM books WHERE id = ? LIMIT 1'; | |
public static $column_names = array ( | |
0 => 'id', | |
1 => 'title', | |
2 => 'subtitle', | |
3 => 'isbn', | |
4 => 'description', | |
5 => 'view', | |
6 => 'published', | |
7 => 'publisher_id', | |
8 => 'published_at', | |
9 => 'is_hot', | |
10 => 'is_selled', | |
); | |
public static $column_hash = array ( | |
'id' => 1, | |
'title' => 1, | |
'subtitle' => 1, | |
'isbn' => 1, | |
'description' => 1, | |
'view' => 1, | |
'published' => 1, | |
'publisher_id' => 1, | |
'published_at' => 1, | |
'is_hot' => 1, | |
'is_selled' => 1, | |
); | |
public static $mixin_classes = array ( | |
); | |
protected $table = 'books'; | |
public $readSourceId = 'default'; | |
public $writeSourceId = 'default'; | |
public $id; | |
public $title; | |
public $subtitle; | |
public $isbn; | |
public $description; | |
public $view; | |
public $published; | |
public $publisher_id; | |
public $published_at; | |
public $is_hot; | |
public $is_selled; | |
public function getSchema() | |
{ | |
if ($this->_schema) { | |
return $this->_schema; | |
} | |
return $this->_schema = SchemaLoader::load('AuthorBooks\\Model\\BookSchemaProxy'); | |
} | |
public static function find($pkId) | |
{ | |
$record = new static; | |
$conn = $record->getReadConnection(); | |
$findStm = $conn->prepare('SELECT * FROM books WHERE id = ? LIMIT 1'); | |
$findStm->setFetchMode(PDO::FETCH_CLASS, 'AuthorBooks\Model\Book'); | |
return static::_stmFetch($findStm, [$pkId]); | |
} | |
public function findByIsbn($value) | |
{ | |
$conn = $this->getReadConnection(); | |
if (!isset($this->_preparedFindStms['isbn'])) { | |
$this->_preparedFindStms['isbn'] = $conn->prepare('SELECT * FROM books WHERE isbn = :isbn LIMIT 1'); | |
} | |
$this->_preparedFindStms['isbn']->execute([':isbn' => $value ]); | |
if (false === ($this->_data = $this->_preparedFindStms['isbn']->fetch(PDO::FETCH_ASSOC)) ) { | |
return $this->reportError("Record not found", [ | |
"sql" => 'SELECT * FROM books WHERE isbn = :isbn LIMIT 1', | |
]); | |
} | |
$this->_preparedFindStms['isbn']->closeCursor(); | |
return $this->reportSuccess( "Data loaded", array( | |
"sql" => 'SELECT * FROM books WHERE isbn = :isbn LIMIT 1', | |
"type" => Result::TYPE_LOAD, | |
)); | |
} | |
public function getId() | |
{ | |
return intval($this->id); | |
} | |
public function getTitle() | |
{ | |
return $this->title; | |
} | |
public function getSubtitle() | |
{ | |
return $this->subtitle; | |
} | |
public function getIsbn() | |
{ | |
return $this->isbn; | |
} | |
public function getDescription() | |
{ | |
return $this->description; | |
} | |
public function getView() | |
{ | |
return intval($this->view); | |
} | |
public function isPublished() | |
{ | |
$value = $this->published; | |
if ($value === '' || $value === null) { | |
return null; | |
} | |
return boolval($value); | |
} | |
public function getPublisherId() | |
{ | |
return intval($this->publisher_id); | |
} | |
public function getPublishedAt() | |
{ | |
return Inflator::inflate($this->published_at, 'DateTime'); | |
} | |
public function isHot() | |
{ | |
$value = $this->is_hot; | |
if ($value === '' || $value === null) { | |
return null; | |
} | |
return boolval($value); | |
} | |
public function isSelled() | |
{ | |
$value = $this->is_selled; | |
if ($value === '' || $value === null) { | |
return null; | |
} | |
return boolval($value); | |
} | |
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, "title" => $this->title, "subtitle" => $this->subtitle, "isbn" => $this->isbn, "description" => $this->description, "view" => $this->view, "published" => $this->published, "publisher_id" => $this->publisher_id, "published_at" => $this->published_at, "is_hot" => $this->is_hot, "is_selled" => $this->is_selled]; | |
} | |
public function setData(array $data) | |
{ | |
if (array_key_exists("id", $data)) { $this->id = $data["id"]; } | |
if (array_key_exists("title", $data)) { $this->title = $data["title"]; } | |
if (array_key_exists("subtitle", $data)) { $this->subtitle = $data["subtitle"]; } | |
if (array_key_exists("isbn", $data)) { $this->isbn = $data["isbn"]; } | |
if (array_key_exists("description", $data)) { $this->description = $data["description"]; } | |
if (array_key_exists("view", $data)) { $this->view = $data["view"]; } | |
if (array_key_exists("published", $data)) { $this->published = $data["published"]; } | |
if (array_key_exists("publisher_id", $data)) { $this->publisher_id = $data["publisher_id"]; } | |
if (array_key_exists("published_at", $data)) { $this->published_at = $data["published_at"]; } | |
if (array_key_exists("is_hot", $data)) { $this->is_hot = $data["is_hot"]; } | |
if (array_key_exists("is_selled", $data)) { $this->is_selled = $data["is_selled"]; } | |
} | |
public function clear() | |
{ | |
$this->id = NULL; | |
$this->title = NULL; | |
$this->subtitle = NULL; | |
$this->isbn = NULL; | |
$this->description = NULL; | |
$this->view = NULL; | |
$this->published = NULL; | |
$this->publisher_id = NULL; | |
$this->published_at = NULL; | |
$this->is_hot = NULL; | |
$this->is_selled = NULL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment