从关系表中获取数据

发布于 2024-12-13 21:03:32 字数 1011 浏览 0 评论 0原文

我正在使用 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:

enter image description here

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-12-20 21:03:32

我阅读了 docu 并认为这可行:

$this->post->get();
foreach ($this->post $post)
{
    foreach ($post->tag->get() as $tag)
    { ... }
}

看起来很棒。我应该自己尝试一下...

更新阅读此处

$p = new Post();
// Get users that are related to the Moderator group
$p->where_related_tag('name', 'php')->get();

I read the docu and think this could work:

$this->post->get();
foreach ($this->post $post)
{
    foreach ($post->tag->get() as $tag)
    { ... }
}

Looks greate. I should give it try myself...

Update, read here:

$p = new Post();
// Get users that are related to the Moderator group
$p->where_related_tag('name', 'php')->get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文