I have a ZF2 application that has database tables for posts and hash tags with a many-to-many relationship. The table definitions are:
CREATE TABLE `hash_tag` (
`id` int(11) NOT NULL,
`tag` varchar(255) NOT NULL
)
CREATE TABLE `post` (
`id` int(11) NOT NULL,
`text` varchar(255)NOT NULL,
`addedOn` datetime NOT NULL
)
CREATE TABLE `post_hashtag` (
`post_id` int(11) NOT NULL,
`hashtag_id` int(11) NOT NULL
)
I've also created models following the patterns explained in the ZF2 documentation's user guide. The HashTag model is as follows:
namespace Application\Model;
use Application\Model\Post;
class HashTag
{
public $id;
public $tag;
protected $posts = array();
public function exchangeArray($data)
{
$this->id = empty($data['id']) ? null : $data['id'];
$this->tag = empty($data['tag']) ? null : $data['tag'];
}
public function getPosts()
{
return $this->posts;
}
public function addPost(Post $post)
{
$this->posts[] = $post;
return $this;
}
}
While the guide gives a pretty good explanation of how to handle basic database models, I'm looking to do something a bit more complicated.
Basically, when I retrieve the hash tag records, I also want to retrieve their associated posts and somehow store them in each individual HashTag object. Doing a normal join query results in a new record for each hash tag AND its accompanying post, which doesn't really help. The documentation doesn't go into any detail about how to do this either, nor can I find anything on Google about it with any decent examples.
I've used Doctrine 2 in the past, and would normally continue to do so. However, I'm preparing for a new job, and my new employer uses Zend\Db. I'm basically doing this to get practice handling database operations without using Doctrine.