使用Containable获取额外信息?

时间:2014-08-22 16:28:54

标签: cakephp model associations

我有4个模特:

Company型号:

class Company extends AppModel {
    public $hasMany = "User";
}

User型号:

class User extends AppModel {
    public $belongsTo = "Company";
    public $hasMany = array("Post", "Comment");
}

Post型号:

class Post extends AppModel {
    public $actsAs = array('Containable');
    public $belongsTo = "User";
    public $hasMany = "Comment";
}

Comment型号:

class Comment extends AppModel {
    public $belongsTo = array("User", "Post");
}

PostsController.php中,我有:

class PostsController extends AppController {

    public function index() {
        $this->set('posts', $this->Post->find('all'));
    }

    public function view($id) {
        //$post = $this->Post->findById($id); // does not retrieve extra information
        //$post = $this->Post->find('first', array('conditions' => array('Post.id' => $id), 'recursive' => 2)); // does retrieve too much extra information
        $post = $this->Post->find('first', array('conditions' => array('Post.id' => $id), 'contain' => array('Company.nom'))); // Warning (512): Model "Post" is not associated with model "Company"
        $this->set('post', $post);
    }
}

在我的Post视图中,我在使用递归方式时进行调试:

Array
(
    [Post] => Array
        (
            [id] => 1
            [user_id] => 1
            [content] => Hello world !
        )

    [User] => Array
        (
            [id] => 1
            [company_id] => 2
            [name] => John DOE
            [Company] => Array
                (
                    [id] => 2
                    [nom] => Apple
                )

            [Post] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [content] => Hello world !
                        )

                )

            [Comment] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [post_id] => 1
                            [content] => This is a comment
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [user_id] => 1
                            [post_id] => 1
                            [content] => This is another comment
                        )

                )

        )

    [Comment] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [user_id] => 1
                    [post_id] => 1
                    [content] => This is a comment
                    [User] => Array
                        (
                            [id] => 1
                            [company_id] => 2
                            [name] => John DOE
                        )

                    [Post] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [content] => Hello world !
                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [user_id] => 1
                    [post_id] => 1
                    [content] => This is another comment
                    [User] => Array
                        (
                            [id] => 1
                            [company_id] => 2
                            [name] => John DOE
                        )

                    [Post] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [content] => Hello world !
                        )

                )

        )

)

如何使用Containable检索Company信息的好方法? 我的问题似乎与this one非常相似。

1 个答案:

答案 0 :(得分:0)

你错过了

public $actsAs = array('Containable');

在您的模型中,例如

class Post extends AppModel {
    public $actsAs = array('Containable');
}

你必须按照答案中给出的链接来实现。我会为你paste it here

而且发现就像

$post = $this->Post->find('first', array('conditions' => array('Post.id' => $id), 
                                         'contain' => array('User' => array('Company'))));

应该阅读相关文档,它有很多例子。

相关问题