I am trying to learn mongodb and decided to build a simple blog application using Zend Framework and Mongodb (using Shanty for ZF https://github.com/coen-hyde/Shanty-Mongo
I have the following document for Post
class App_Model_Post extends Shanty_Mongo_Document
{
protected static $_db = 'blog';
protected static $_collection = 'posts';
protected static $_requirements = array(
'Title' => 'Required',
'Content' => 'Required',
'CreatedOn' => 'Required',
'Author' => array('Document:App_Model_User', 'Required', 'AsReference'),
'Slug' => 'Required',
);
public function preSave()
{
$title = $this->Title;
$this->Slug = strtolower($title);
}
}
and document for Comment
class App_Model_Comment extends Shanty_Mongo_Document
{
protected static $_db = 'blog';
protected static $_collection = 'comments';
protected static $_requirements = array(
'Content' => 'Required',
'Author' => array('Required', 'Document:App_Model_User', 'AsReference'),
'Post' => array('Required', 'Document:App_Model_Post'),
);
}
And in my controller I do the following to get the Post and Comments for this Post
public function viewAction()
{
$slug = $this->_getParam('id');
$post = App_Model_Post::one(array('Slug' => $slug));
$this->view->post = $post;
$this->view->title = 'Post Title : ' . $post->Title;
$comments = App_Model_Comment::all(array('Post.Slug' => $post->Slug))->skip(0)->limit(10);
$this->view->comments = $comments;
}
I wonder if this is a good way to accomplish this. I mean is it good practice to reference to Post in the Comment or should I inject Comment documents to Post document?
Thanks.