Skip to content

Instantly share code, notes, and snippets.

@hussani
Last active December 14, 2015 04:49
Show Gist options
  • Save hussani/5030793 to your computer and use it in GitHub Desktop.
Save hussani/5030793 to your computer and use it in GitHub Desktop.
Post entity with Respect/Relational
<?php
namespace Hussani\Entity;
use Respect\Relational\Mapper;
use PDO;
use DateTime;
class Post extends AbstractEntity
{
protected $title;
protected $content;
protected $date;
protected $last_update;
protected $status;
protected $author;
protected $categories = array();
protected $tags = array();
protected $mapper;
public function __construct($id)
{
$this->mapper = new Mapper(new PDO('mysql:host=127.0.0.1;port=3306;dbname=blog','root',''));
if (is_numeric($id)) {
$this->load($id);
}
}
public function load($id)
{
if (!is_numeric($id)) {
return false;
}
$post = $this->mapper->post[$id]->user->fetch();
if (!is_numeric($post->id)) {
return false;
}
$this->id = $post->id;
$this->title = $post->title;
$this->content = $post->content;
$this->date = new DateTime($post->date);
$this->last_update = new DateTime($post->last_update);
$this->status = $post->status;
$this->author = $post->user_id;
}
/**
* Getter for title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Setter for title
*
* @param mixed $title Value to set
* @return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Getter for content
*
* @return mixed
*/
public function getContent()
{
return $this->content;
}
/**
* Setter for content
*
* @param mixed $content Value to set
* @return self
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Getter for author
*
* @return mixed
*/
public function getAuthor()
{
return $this->author;
}
/**
* Setter for author
*
* @param mixed $author Value to set
*
* @return self
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Getter for categories
*
* @return array
*/
public function getCategories()
{
if (!empty($this->categories)) {
return $this->categories;
}
if (!is_numeric($this->id)) {
return array();
}
return $this->mapper
->category->post_category->post[$this->id]->fetchAll();
}
/**
* Getter for tags
*
* @return array
*/
public function getTags()
{
if (!empty($this->tags)) {
return $this->tags;
}
if (!is_numeric($this->id)) {
return array();
}
return $this->mapper
->tag->post_tag->post[$this->id]->fetchAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment