CakePhp 模型关系不提供反向引用

发布于 2024-10-28 04:56:31 字数 1929 浏览 1 评论 0原文

我的模型关系是按以下方式设置的

class Post extends AppModel {
    var $name = 'Post';
    var $displayField = 'title';
    var $hasMany = array('Comment');
    var $belongsTo = array('User');
 }

 class User extends AppModel {
    var $name = 'User';
    var $displayField = 'username';
    var $hasMany = array('Post', 'Comment');
 }

 class Comment extends AppModel {
    var $name = 'Comment';
    var $displayField = 'id';
    var $belongsTo = array('User', 'Post');
 }

目前,当我尝试在帖子视图的评论部分引用用户名时, 它错误地指出无效索引,经过进一步调查,我注意到我的模型不包括用户信息作为我的评论的反向参考,仅帖子中包含用户信息,

结果数组如下所示:

Array
(
    [Post] => Array
        (
            [id] => 1
            [title] => test post
            [user_id] => 1
            [body] => this is a test post 
            [date_posted] => 0000-00-00 00:00:00
            [url_slug] => this-is-a-test-post
        )

    [User] => Array
        (
            [id] => 1
            [username] => admin
            [password] => e7b9f7bc09beee85947ef987d7df49df136c7c38
            [first_name] => Chris
            [last_name] => McGrath
            [roles] => Admin
            [email] => [email protected]
            [last_login] => 0000-00-00 00:00:00
            [member_since] => 0000-00-00 00:00:00
            [FacebookProfile] => 
            [TwitterUserName] => 
        )

    [Comment] => Array
        (
            [0] => Array
                (
                    [id] => 5
                    [post_id] => 1
                    [user_id] => 1  //expecting this to pull user model for each 
                    [comment] => test comment
                )

        )

)

我的问题是这是由我在模型关联中做错的事情引起的,这是模型框架的限制还是我应该在这种类型的实例中从用户模型中手动获取用户

currently my model relation ships are set up in the following fashion

class Post extends AppModel {
    var $name = 'Post';
    var $displayField = 'title';
    var $hasMany = array('Comment');
    var $belongsTo = array('User');
 }

 class User extends AppModel {
    var $name = 'User';
    var $displayField = 'username';
    var $hasMany = array('Post', 'Comment');
 }

 class Comment extends AppModel {
    var $name = 'Comment';
    var $displayField = 'id';
    var $belongsTo = array('User', 'Post');
 }

when i am trying to refer to the username in the comments section on my posts view
it errors out saying invalid index and upon further inestigation i have noticed that my model is not including the user info as a back reference for my comments only the user information is being included for the posts

the resulting array looks like this:

Array
(
    [Post] => Array
        (
            [id] => 1
            [title] => test post
            [user_id] => 1
            [body] => this is a test post 
            [date_posted] => 0000-00-00 00:00:00
            [url_slug] => this-is-a-test-post
        )

    [User] => Array
        (
            [id] => 1
            [username] => admin
            [password] => e7b9f7bc09beee85947ef987d7df49df136c7c38
            [first_name] => Chris
            [last_name] => McGrath
            [roles] => Admin
            [email] => [email protected]
            [last_login] => 0000-00-00 00:00:00
            [member_since] => 0000-00-00 00:00:00
            [FacebookProfile] => 
            [TwitterUserName] => 
        )

    [Comment] => Array
        (
            [0] => Array
                (
                    [id] => 5
                    [post_id] => 1
                    [user_id] => 1  //expecting this to pull user model for each 
                    [comment] => test comment
                )

        )

)

my question is is this being caused by something i am doing incorrectly in the model associations, is this a limitation of the model framework or should i be grabbing user manually from the user model in this type of instance

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

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

发布评论

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

评论(2

茶底世界 2024-11-04 04:56:31

尝试做这样的事情:

$this->Post->recursive = 2; //This will get all the comments and their relationships
$post = $this->Post->read(null,$post_id); //The way you were getting your post.
debug($post); // You will see what it gets

编辑:
你也可以尝试这个:

$this->Post->Comment->recursive = 2;

Try doing something like this:

$this->Post->recursive = 2; //This will get all the comments and their relationships
$post = $this->Post->read(null,$post_id); //The way you were getting your post.
debug($post); // You will see what it gets

EDIT:
You can try this too:

$this->Post->Comment->recursive = 2;
静水深流 2024-11-04 04:56:31

使用 recursive = 2 从来都不是一个好主意。

$this->Post->find(
    'first',
    array(
        'conditions' => array(
            'Post.id' => $id
        ),
        'contain' => array(
            'User',
            'Comment' => array(
                'User' => array(
                    'fields' => array(
                        'User.id',
                        'User.name'
                    )
                )
            )
        )
    )
);

http://book.cakephp.org/view/1323/Containable

using recursive = 2 is never a good idea.

$this->Post->find(
    'first',
    array(
        'conditions' => array(
            'Post.id' => $id
        ),
        'contain' => array(
            'User',
            'Comment' => array(
                'User' => array(
                    'fields' => array(
                        'User.id',
                        'User.name'
                    )
                )
            )
        )
    )
);

http://book.cakephp.org/view/1323/Containable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文