php - How to push a mysql result's fields up to become the property of a class -
how can push mysql result result's fields become property of class?
for instance, result,
object(articlemodel)[4] public 'item' => object(stdclass)[7] public 'article_id' => string '4' (length=1) public 'parent_id' => string '4' (length=1) public 'template_id' => string '4' (length=1) public 'type' => string 'page' (length=4) public 'url' => string 'home' (length=4) public 'title' => string 'home' (length=4) ... , on...
i want this,
object(stdclass)[7] public 'article_id' => string '4' (length=1) public 'parent_id' => string '4' (length=1) public 'template_id' => string '4' (length=1) public 'type' => string 'page' (length=4) public 'url' => string 'home' (length=4) public 'title' => string 'home' (length=4) ... , on...
model, mapper, service below classes how result above ( object(articlemodel)[4]public 'item' => [...]
).
model,
class articlemodel { // not ideal if have set property manually. public $article_id; public $title; public $url; ... , on... }
mapper,
class articlemapper { private $database; public function __construct(database $pdo) { $this->database = $pdo; } public function getrow($article_id) { $sql = " select * article p article_id = ? "; $this->item = $this->database->fetchrowobject($sql, $article_id); return $this; } public function addcontent() { ... } public function maprow(articlemodel $articlemodel, $article_id) { $articlemodel->item = $this->getrow($article_id)->item; } }
service,
class articleservice { public function __construct(database $pdo) { $this->database = $pdo; } public function fetchrow($article_id) { $articlemodel = new articlemodel(); $articlemapper = new articlemapper($this->database); $articlemapper->maprow($articlemodel,$article_id); return $articlemodel; } }
view/ controller,
// instance of database. $database = new database(dsn,db_user,db_pass); // make connection. $database->connect(); $articleservice = new articleservice($database); $article = $articleservice->fetchrow(4); var_dump($article);
result,
object(articlemodel)[4] public 'item' => object(stdclass)[7] public 'article_id' => string '4' (length=1) public 'parent_id' => string '4' (length=1) public 'template_id' => string '4' (length=1) public 'type' => string 'page' (length=4) public 'url' => string 'home' (length=4) public 'title' => string 'home' (length=4) ... , on...
i following slideshare example make example above.
Comments
Post a Comment