How to push a mysql result's fields up to become the property of a class

114 views Asked by At

How can I push a mysql result result's fields up to become the property of a class?

For instance, this is my 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)
          ... and so on...

I want it to be like 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)
  ... and so on...

Model, Mapper, Service below are the classes how I get to the result above ( object(ArticleModel)[4]public 'item' => [...] ).

model,

class ArticleModel
{
    // It is not ideal if I have to set the property manually.
    public $article_id;
    public $title;
    public $url; 
    ... and so on...
}

mapper,

class ArticleMapper
{
    private $Database;

    public function __construct(Database $pdo)
    {
        $this->Database = $pdo;
    }

    public function getRow($article_id)
    {
        $sql = "
            SELECT *
            FROM article AS p
            WHERE 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)
      ... and so on...

I am following this slideshare example to make my example above.

0

There are 0 answers