自联接循环视图?

时间:2012-03-01 05:03:28

标签: cakephp-1.3

我有一个数据集,它的功能似乎应该是(下):

Array
(
[0] => Array
    (
        [Collaboration] => Array
            (
                [id] => 18
                [parent_id] => 
                [created] => 2012-01-14 20:17:00
                [title] => Title 1
                [message] => Lorem ipsum dolor sit amet major 1.
                [type] => Competition
                [0] => Array
                    (
                        [id] => 100
                        [parent_id] => 18
                        [created] => 2012-02-29 14:07:23
                        [title] => Title sub 1
                        [message] => Lorem ipsum dolor sit amet sub 1.
                        [type] => Message 
                    )

                [1] => Array
                    (
                        [id] => 101
                        [parent_id] => 18
                        [created] => 2012-02-29 14:07:23
                        [title] => Title sub 2
                        [message] => Lorem ipsum dolor sit amet sub 2.
                        [type] => Message
                    )

            )

        [Registration] => Array
            (
            )

    )

[1] => Array
    (
        [Collaboration] => Array
            (...

我可以在视图中呈现此自连接数据集的方式是什么?我可以这样看数据

<table>
        <tr>
        <td><?php echo $collaboration['Collaboration'][0]['message'];?></td>           
        </tr>
</table>

但当然不应该使用这个数组的方式。如何循环通过这个内部数组?

谢谢,

1 个答案:

答案 0 :(得分:0)

模型是:

var $hasMany = array('Registration','Collaboration' => array('foreignKey' => 'parent_id'));

和控制器:

function message_board() {
$this->paginate['Collaboration'] = array(
'conditions' 
=> array('Collaboration.to_name' => array('Extranet', 'Internet'), 'AND' 
=> array('Collaboration.company_id' => Configure::read('company'), 'AND' 
=> array('Collaboration.parent_id' => null))),
'limit' => 10,
'order' => 'Collaboration.created DESC'
);

和表格是:

id               int(11) PK
parent_id        int(11)
created          datetime
title            varchar(1000)
message          longtext...

// Jari