Archived
1
0
Fork 0
This repository has been archived on 2026-05-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
fiktivkod/application/models/Mapper/BlogPost.php
2010-10-20 20:33:23 +02:00

104 lines
No EOL
2.5 KiB
PHP

<?php
class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract
{
protected $_mapperUser = null;
public function __construct($dbtable = null) {
parent::__construct($dbtable);
$this->_mapperUser = new Mapper_User(new Table_User());
}
protected function _createBlogPosts($obj)
{
$posts = array();
foreach($obj as $post)
$posts[] = $this->_createBlogPost($post);
return $posts;
}
protected function _createBlogPost($obj)
{
if ($obj instanceof Zend_Db_Table_Row)
$obj = $obj->toArray();
if ($obj instanceof stdClass)
$obj = (array) $obj;
if (is_array($obj)) {
if (isset($obj['userId'])) {
$obj['author'] = Fiktiv_Data_Service::getInstance()->User->findById($obj['userId']);
unset($obj['userId']);
}
$blogPost = new BlogPost($obj);
} else {
$blogPost = null;
}
return $blogPost;
}
public function findById($id)
{
if (is_numeric($id)) {
return $this->_createBlogPost($this->_dbTable->find($id)->current());
}
return null;
}
public function findAllPostByAuthor($user)
{
if ($user instanceof User) {
return $this->_createBlogPost($this->_dbTable->findByUser($user->getId()));
}
}
public function findByMonth($month = null)
{
return $this->_dbTable->select()
->from($this->_dbTable->_name,array('Month' => 'DATE_FORMAT(pubDate,"%Y %M")', 'Date' => 'DATE_FORMAT(pubDate,"%Y-%M")'))
->group('Month')
->query()
->fetchAll();
}
public function findByDate($date)
{
$date = new Fiktiv_Date($date);
$where = $this->quoteInto('pubDate LIKE ?', $date->getIso());
return $this->_createBlogPosts($this->_dbTable->fetchAll($where));
}
public function findAll($limit = null)
{
if (!is_numeric($limit))
throw new Fiktiv_Exception('limit must be numeric');
return $this->_createBlogPosts($this->_dbTable->fetchAll(null, 'pubDate DESC', $limit));
}
public function save(BlogPost $post)
{
$data = $post->toArray();
unset($data['id']);
return $this->_dbTable->update(
$data,
$this->quoteInto('id = ?',$post->getId())
);
}
}