我正在用CodeIgniter和DataMapper编写简单的博客,我对关系有问题。如何使用DattaMapper通过特殊标签获取帖子。 SQL查询将是这样的:
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代码:
<?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();
}
}
数据库方案:
有什么建议吗?