1

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();
    }
}

データベーススキーム:

ここに画像の説明を入力

助言がありますか?

4

1 に答える 1

1

ドキュメントを読んで、これでうまくいくと思います:

$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();
于 2011-11-07T16:44:45.403 に答える