models additions and changes
This commit is contained in:
parent
6b9366db54
commit
4c2c435f19
4 changed files with 92 additions and 41 deletions
|
|
@ -10,7 +10,8 @@ class BlogPost extends Fiktiv_Model_Abstract
|
|||
'content' => null,
|
||||
'pubDate' => null,
|
||||
'author' => null,
|
||||
'isPublished' => false
|
||||
'isPublished' => false,
|
||||
'tags' => array(),
|
||||
);
|
||||
|
||||
public function setAttribs(array $data) {
|
||||
|
|
@ -30,6 +31,9 @@ class BlogPost extends Fiktiv_Model_Abstract
|
|||
case 'pubdate':
|
||||
$this->setPubDate(new Zend_Date($value));
|
||||
break;
|
||||
case 'tags':
|
||||
$this->setTags($value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -73,6 +77,28 @@ class BlogPost extends Fiktiv_Model_Abstract
|
|||
return false;
|
||||
}
|
||||
|
||||
public function addTag($tag)
|
||||
{
|
||||
if (is_string($tag)) {
|
||||
$this->_data['tags'][] = $tag;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setTags(array $tags)
|
||||
{
|
||||
// make sure the all elements are strings
|
||||
foreach ($tags as $tag) {
|
||||
if (!is_string($tag))
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->_data['tags'] = $tags;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setPubDate($date)
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -28,15 +28,23 @@ class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract
|
|||
|
||||
protected function _createBlogPost($obj)
|
||||
{
|
||||
$blogPost = new BlogPost();
|
||||
|
||||
if ($obj instanceof Zend_Db_Table_Row) {
|
||||
if ($obj instanceof Zend_Db_Table_Row)
|
||||
$obj = $obj->toArray();
|
||||
|
||||
|
||||
$blogPost->setId($obj->id);
|
||||
$blogPost->setTitle($obj->title);
|
||||
$blogPost->setContent($obj->content);
|
||||
$blogPost->setPubDate(new Fiktiv_Date($obj->pubDate));
|
||||
$blogPost->setAuthor($this->_mapperUser->findById($obj->userId));
|
||||
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;
|
||||
|
|
@ -78,5 +86,15 @@ class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract
|
|||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
|
||||
public function save(BlogPost $post)
|
||||
{
|
||||
$data = $post->toArray();
|
||||
unset($data['id']);
|
||||
|
||||
return $this->_dbTable->update(
|
||||
$data,
|
||||
$this->_dbTable->getAdapter()->quoteInto('id = ?',$post->getId())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,15 +5,21 @@
|
|||
*/
|
||||
class User extends Fiktiv_Model_Abstract
|
||||
{
|
||||
const AVATAR_NONE = 0;
|
||||
const AVATAR_FIKTIV = 1;
|
||||
const AVATAR_GRVTAR = 2;
|
||||
|
||||
protected $_data = array();
|
||||
|
||||
protected $_default = array(
|
||||
'id' => 0,
|
||||
'email' => null,
|
||||
'firstName' => null,
|
||||
'lastName' => null,
|
||||
'regDate' => null,
|
||||
'isDeleted' => false
|
||||
'id' => 0,
|
||||
'email' => null,
|
||||
'firstName' => null,
|
||||
'lastName' => null,
|
||||
'regDate' => null,
|
||||
'isDeleted' => false,
|
||||
'avatar' => self::AVATAR_NONE,
|
||||
'avatarImage' => null,
|
||||
);
|
||||
|
||||
public function setId($id)
|
||||
|
|
@ -97,6 +103,27 @@ class User extends Fiktiv_Model_Abstract
|
|||
return true;
|
||||
}
|
||||
|
||||
public function setAvatar($type)
|
||||
{
|
||||
|
||||
if (self::AVATAR_NONE === $type) {
|
||||
|
||||
$this->_data['avatar'] = self::AVATAR_NONE;
|
||||
$this->_data['avatarImage'] = null;
|
||||
|
||||
} else if (self::AVATAR_GRVTAR === $type) {
|
||||
|
||||
$this->_data['avatar'] = self::AVATAR_GRVTAR;
|
||||
$this->_data['avatarImage'] = null;
|
||||
|
||||
} else if (is_string($type)) {
|
||||
|
||||
$this->_data['avatar'] = self::AVATAR_FIKTIV;
|
||||
$this->_data['avatarImage'] = $type;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function setPassword($password)
|
||||
{
|
||||
if (!is_string($password))
|
||||
|
|
@ -142,28 +169,4 @@ class User extends Fiktiv_Model_Abstract
|
|||
return $this->_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load user data from database (restore last saved)
|
||||
*/
|
||||
public function reload()
|
||||
{
|
||||
$userData = Fiktiv_Data_Service::getInstance()->User->findById($this->getId());
|
||||
|
||||
if ($userData instanceof User) {
|
||||
|
||||
$this->setAttribs($userData->toArray());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save User
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
return Fiktiv_Data_Service::getInstance()->User->save($this);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
abstract class Fiktiv_Model_Abstract
|
||||
{
|
||||
|
||||
protected $_default = array();
|
||||
|
||||
public function __construct($data = array())
|
||||
|
|
@ -55,5 +54,10 @@ abstract class Fiktiv_Model_Abstract
|
|||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function save()
|
||||
{
|
||||
$class = get_class($this);
|
||||
return Fiktiv_Data_Service::getInstance()->$class->save($this);
|
||||
}
|
||||
}
|
||||
Reference in a new issue