I am writing simple blog with CodeIgniter and DataMapper and I have problem with relation. How can I get posts by particularly tags with DattaMapper. SQL query will be something like that:
SELECT
posts.id,
posts.title,
posts.content,
posts.created,
tags.name
FROM
posts,
posts_tags,
tags
WHERE
posts.id = posts_tags.post_id AND
posts_tags.tag_id = tags.id AND
tag.name = 'php';
ORDER BY
posts.created DESC;
php code:
<?php
class Post extends DataMapper
{
public $has_many = array('tag');
public function __construct()
{
parent::__construct();
}
public function getPostsByTags($name, $offset)
{
// this doesn't work
// $this->get_where(array('tags.name', $name), 3, $offset);
}
}
class Tag extends DataMapper
{
public $has_many = array('post');
public function __construct()
{
parent::__construct();
}
}
Database scheme:
Any suggestions?