1

好吧,在我的 cakephp 项目中,我有 6 个模型,它们是 {User,Property,Category,Status,Comments,Attachable}

属性模型: $belongsTo = {User,Category,Status} 。和 $hasMany = {评论,可附加} ..

属性控制器索引方法..

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 手动查询评论和附加模型。

这就是查询的样子

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 循环..即

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

可以这样解决吗?或如何?请指导..谢谢..

4

1 回答 1

1

两件事情:

  1. 您可能可以通过使用Containable 行为来解决您的问题。它非常灵活,可以与 pagination一起使用。
  2. 建立inner join一对多关系可能不是一个好主意。
于 2012-07-15T11:29:07.687 回答