了解cakephp hasAndBelongsToMany

时间:2014-04-13 11:52:53

标签: php cakephp has-and-belongs-to-many

我尝试使用用户模型检索所有电影评论。我正在关注http://joshbenner.me/blog/understanding-cakephp-associations/教程。

$movie = $this->Movie->read(null, $id);

它返回结构

Array
(
    [Movie] => Array
        (
            [id] => 1
            [title] => movie1
            [release_date] => 2014-04-10 00:00:00
            [director_id] => 1
        )
    [Director] => Array
        (
            [id] => 1
            [first_name] => direct
            [last_name] => direc
        )

    [Review] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [user_id] => 1
                    [body_text] => body_text 22 body_text
                    [ReviewsMovie] => Array
                        (
                            [id] => 1
                            [movie_id] => 1
                            [review_id] => 2
                        )

                )

            [1] => Array
                (
                    [id] => 1
                    [user_id] => 1
                    [body_text] => s body_textbody_text
                    [ReviewsMovie] => Array
                        (
                            [id] => 2
                            [movie_id] => 1
                            [review_id] => 1
                        )

                )

        )

)

我也想检索用户模型。所以,我可以显示用户名和其他信息。发表评论的人

3 个答案:

答案 0 :(得分:2)

如果我对你的关系方案看起来像这样:

评论 - > BelongsTo - >用户 和评论 - >属于也 - >电影

所以最好的做法是创建一个名为ReviewUser的新表 " hasMany through(The Join Model)"像这样:

id
user_id
movie_id
etc...

ReviewUser-> belongsto -> (User, Movie)
User-> hasMany -> ReviewUser
Movie-> hasMany -> ReviewUser

注意:这是一种很好的技术,你需要在关联表中存储更多的嵌入信息。

因此您现在可以使用条件语句FORM review WHERE user_id = xxx

选择评论

请参阅:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

答案 1 :(得分:1)

您只需要在Review模型上定义另一种关系。

Review.php

添加此关系 -

 public $belongsTo = array('User');

答案 2 :(得分:0)

它返回Array (     [电影] =>排列         (             [id] => 1             [title] =>电影             [release_date] => 2014-04-23 00:00:00             [director_id] => 1         )

[Director] => Array
    (
        [id] => 1
        [first_name] => direcot
        [last_name] => or
    )

[Review] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [movie_id] => 1
                [author_name] => dsf
                [body_text] => sdas
            )

    )

[UserReview] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [movie_id] => 1
            )

    )

我希望它返回

阵 (     [电影] =>排列         (             [id] => 1             [title] =>电影             [release_date] => 2014-04-23 00:00:00             [director_id] => 1         )

[Director] => Array
    (
        [id] => 1
        [first_name] => direcot
        [last_name] => or
    )

[Review] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1

['用户'] => array(这里的用户数组)

                [movie_id] => 1
                [author_name] => dsf
                [body_text] => sdas
            )

    )

[UserReview] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [movie_id] => 1
            )

    )