Cakephp模型协会加入

时间:2012-07-15 09:31:39

标签: cakephp join model associations

好吧,在我的cakephp项目中,我有6个模型,它们是{用户,属性,类别,状态,评论,可附加}

属性模型:$ belongsTo = {用户,类别,状态}。 AND $ hasMany = {Comment,Attachable} ..

属性控制器索引方法..

public function index() {
    $this->Property->recursive = 0;
    $this->set('properties', $this->paginate());
    $properties= $this->paginate();
    //pr($properties);
    //exit;
}

输出:

Array
(
    [0] => Array
        (
            [Property] => Array
                (
                    [id] => 1
                    [user_id] => 1
                    [category_id] => 1
                )

            [User] => Array
                (
                    [id] => 1
                    [name] => zals
                    [username] => admin
                    [userLevel] => 1
                )

            [Category] => Array
                (
                    [id] => 1
                    [name] => villa
                    [property_count] => 0
                )

            [Status] => Array
                (
                    [id] => 1
                    [name] => New
                    [property_count] => 0
                )
        )

    [1] => Array
        (
            [Property] => Array
                (
                    [id] => 2
                    [user_id] => 1
                    [phone] => 78666
                )

            [User] => Array
                (
                    [id] => 1
                    [name] => zals
                    [username] => admin
                )

            [Category] => Array
                (
                    [id] => 1
                    [name] => villa
                    [description] => villas are iby a wealthy person.
                    [property_count] => 0
                )

            [Status] => Array
                (
                    [id] => 1
                    [name] => New
                    [description] => New Property
                    [property_count] => 0
                )

        )

    [2] => Array
        (
            [Property] => Array
                (
                    [id] => 3
                    [user_id] => 1
                    [category_id] => 1
                )

            [User] => Array
                (
                    [id] => 1
                    [name] => zals
                    [username] => admin
                    [email] => admin@realty.com
                    [userLevel] => 1
                )

            [Category] => Array
                (
                    [id] => 1
                    [name] => villa
                    [property_count] => 0
                )

            [Status] => Array
                (
                    [id] => 1
                    [name] => New
                    [property_count] => 0
                )

        )
)

以上方法只从$ belongsTo变量中的模型中检索数据。我想要的是将相关的评论和可附加模型与相同的组合 查询。这可以通过LEFT JOIN手动查询注释和可附加的MODEL。

这是查询的样子

function index() {
    $properties = $this->Property->query("SELECT `Property`.`id`, `Property`.`user_id`, `Property`.`category_id`, `Property`.`status_id`, `Property`.`state_id`, `User`.`id`, `User`.`name`, `User`.`username`,`Category`.`id`, `Category`.`name`, `Category`.`description`, `Status`.`name`, `Status`.`description``Comment`.`id`,`Comment`.`name`,`Comment`.`comment`,`Attachment`.`id`,`Attachment`.`AttachmentName` FROM `properties` AS `Property`
        LEFT JOIN `users` AS `User` ON (`Property`.`user_id` = `User`.`id`)
        LEFT JOIN `categories` AS `Category` ON (`Property`.`category_id` = `Category`.`id`)
        LEFT JOIN `statuses` AS `Status` ON (`Property`.`status_id` = `Status`.`id`)
        LEFT JOIN `cities` AS `City` ON (`Property`.`city_id` = `City`.`id`)
        LEFT JOIN `comments` AS `Comment` ON (`Property`.`id` = `Comment`.`id`)
        LEFT JOIN `attachments` AS `Attachment` ON (`Property`.`id` = `Attachment`.`id`) WHERE 1 = 1 LIMIT 20");
    //pr($properties);
    //exit;
    $this->set('properties',$properties);   
}

输出:

Array
(
    [0] => Array
        (
            [Property] => Array
                (
                    [id] => 1
                    [user_id] => 1
                    [category_id] => 1
                    [status_id] => 1
                )
            [User] => Array
                (
                    [id] => 1
                    [name] => za
                    [username] => admin
                )
            [Category] => Array
                (
                    [id] => 1
                    [name] => villa
                    [description] => villa by a wealthy person.
                )
            [Status] => Array
                (
                    [name] => New
                )
            [Comment] => Array
                (
                    [id] => 1
                    [name] => A
                    [comment] => hello
                )
            [Attachment] => Array
                (
                    [id] => 1
                    [AttachmentName] => 1342009083_4c2380.jpg
                )
        )
    [1] => Array
        (
            [Property] => Array
                (
                    [id] => 2
                    [user_id] => 1
                    [category_id] => 1
                    [status_id] => 1
                )
            [User] => Array
                (
                    [id] => 1
                    [name] => zals
                    [username] => admin
                )
            [Category] => Array
                (
                    [id] => 1
                    [name] => villa
                    [description] => villas a
                )
            [Status] => Array
                (
                    [name] => New
                )
            [Comment] => Array
                (
                    [id] => 2
                    [name] => asdasd
                    [comment] => asdasdas
                )
            [Attachment] => Array
                (
                    [id] => 2
                    [AttachmentName] => 92f2e3c067d731a3823762.jpg
                )
        )
)

我确信后一种方式不是正确的方法。或
假设我在控制器中使用了cakephp的默认索引方法,而在VIEW中我可以在主foreach循环中执行嵌套的foreach循环.. i .e

<?php foreach ($properties as $property) { ?>
    $id = $property['Property']['id'];
    $comments = ..... //here to query for the associated comments passing the id value .. 
?>  

这可以这样解决吗?或者怎么样?请指导..谢谢..

1 个答案:

答案 0 :(得分:1)

两件事:

  1. 您可以使用Containable behaviour解决问题。它非常灵活,可以是used with pagination
  2. 对一对多关系进行inner join可能不是一个好主意。