3

我想实现一个结构,例如一个组织有很多部门,部门有很多人。

我已经像这样设置了我的模型结构:

组织

<?php

class Organisation extends AppModel {

    public $hasMany = array(
            'Department' => array(
                'className' => 'Department',
                'foreignKey' => 'organisations_id'
            )
        );
}

部门

<?php

class Department extends AppModel {

    public $hasMany = array(
            'Person' => array(
                'className' => 'Person',
                'foreignKey' => 'departments_id'
            )
        );
}

<?php

class Person extends AppModel {

}

然后我有一个这样的控制器:

<?php

class OrganisationsController extends AppController {

    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');


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

}

当我打印出 $organisations 时,我得到一个这样的数组:

Array
(
    [0] => Array
        (
            [Organisation] => Array
                (
                    [id] => 1
                    [created] => 2013-01-03 16:02:47
                )

            [Department] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [created] => 2013-01-03 16:02:47
                            [organisations_id] => 1
                        )

                )

        )

)

我是 PHP 和 CakePHP 的新手,但您不希望 Person 数组包含在 Organization 数组中吗?如果没有,是否有另一种方法可以实现上述结构(组织->部门->人员)?

非常感谢任何有关如何解决此问题的提示!:)

4

2 回答 2

3

You are probably looking for recursive

Or you could make use of the containable behaviour

But please have a look at the result. When using recursive you can get a lot of data you don't want! So please be careful and select the fields you actually need!

Recursive

You would get something like:

<?php

class OrganisationsController extends AppController {

    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');


    public function index() {
        $this->Organisation->recursive = 2; # or -1, 0, 1, 2, 3
        $this->set('organisations', $this->Organisation->find('all'));
    }
}

You could also declare this in the find itself like so:

$this->set('organisations', $this->Organisation->find('all' array(
        'recursive' => 2 # or -1, 0, 1, 2, 3
    )
));

Containable

class Organisation extends AppModel {

    public $hasMany = array(
        'Department' => array(
            'className' => 'Department',
            'foreignKey' => 'organisations_id'
        )
    );

    $actsAs = array('Containable');
}

Now in your controller you can do something like:

$this->set('organisations', $this->Organisation->find('all', array(
        'contain' => array('User')
    )
));

But as always, there are many roads leading to Rome. So please read the books very carefully!

于 2013-01-04T09:31:08.550 回答
1

递归函数将为您完成。

就在 OrganisationsController 索引函数中,尝试如下操作。

  $this->Organisation->recursive = 2;
  $this->set('organisations', $this->Organisation->find('all'));

注意:递归可能会影响你的性能,你可以使用unbind方法来摆脱它,只需获取你想要的数据。

于 2013-01-04T09:30:17.893 回答