从关系表中获取数据
我正在使用 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();
}
}
数据库方案:
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我阅读了 docu 并认为这可行:
看起来很棒。我应该自己尝试一下...
更新,阅读此处:
I read the docu and think this could work:
Looks greate. I should give it try myself...
Update, read here: