CakePHP 递归不适用于 loadModel 和 this->set

CakePHP Recursive not working with loadModel and this->set

当我使用 $this->loadModel 时递归似乎不起作用,我想说我记得在这种情况下必须显式设置 belongsTo 和 hasMany,但这似乎是反 cakephp-auto-神奇,我还没有找到说明如何使用的任何答案

$this->loadModel 

$this->...->find('all', array('recursive' => 2));  

非常感谢任何关于为什么这不起作用的想法。

$this->loadModel('DeployablesJoin');
$deployablesJoins = $this->DeployablesJoin->find('all', array('recursive' => 2));

$this->loadModel('DeployablesComplete');
$deployablesCompletes = $this->DeployablesComplete->find('all', array('recursive' => 2, 'conditions' => array('successful' => array('0', '-1'))));

$this->set('deployablesCompletes', $deployablesCompletes);
$this->set('deployablesJoins', $deployablesJoins);

编辑:完整代码如下

我的 DeployablesJoin 模型

class DeployablesJoin extends NodeCncAppModel {

    public $actsAs = array('Containable');

    public $belongsTo = array(
        'Deployable' => array(
            'className' => 'Deployable',
            'foreignKey' => 'deployable_id',
            'conditions' => '',
            'fields' => 'title,filename,created',
            'order' => ''
        ),
        'ServerClass' => array(
            'className' => 'ServerClass',
            'foreignKey' => 'server_class_id',
            'conditions' => '',
            'fields' => 'name',
            'order' => ''
        )
    );
}

控制器相关部分

    $this->loadModel('DeployablesJoin');
    $deployablesJoins = $this->DeployablesJoin->find('all');

    print_r($deployablesJoins); die();

结果来自 print_r/die

Array ( 
    [0] => Array ( 
        [DeployablesJoin] => Array ( 
            [id] => 1 
            [deployable_id] => 5 
            [server_class_id] => 1 
        ) 
    ) 
)

我发现以下是有效的(相关部分控制器)

$this->loadModel('DeployablesJoin');
$this->DeployablesJoin->belongsTo = array(
    'Deployable' => array(
        'className' => 'Deployable',
        'foreignKey' => 'deployable_id',
        'conditions' => '',
        'fields' => 'title,filename,created',
        'order' => ''
    ),
    'ServerClass' => array(
        'className' => 'ServerClass',
        'foreignKey' => 'server_class_id',
        'conditions' => '',
        'fields' => 'name',
        'order' => ''
    )
);
$deployablesJoins = $this->DeployablesJoin->find('all');

print_r($deployablesJoins); die();

结果来自 print_r/die

Array ( 
    [0] => Array ( 
        [DeployablesJoin] => Array ( 
            [id] => 1 
            [deployable_id] => 5 
            [server_class_id] => 1 
        ) 
        [Deployable] => Array ( 
            [title] => asdf 
            [filename] => 5_agent.zip 
            [created] => 2015-01-09 21:31:25 
        ) 
       [ServerClass] => Array ( 
            [name] => Crawler 
       ) 
 )

当我在 $this->DeployablesJoin 上执行 print_r/die 时,我得到以下内容

AppModel Object
(
    [useDbConfig] => default
    [useTable] => deployables_joins
    [id] => 
    ...
    [table] => deployables_joins
    [primaryKey] => id
    ...
    [name] => DeployablesJoin
    [alias] => DeployablesJoin
    [tableToModel] => Array
        (
            [deployables_joins] => DeployablesJoin
        )

    [cacheQueries] => 
    [belongsTo] => Array
        (
        )

除了关联设置不正确之外,您的 'recursive' 似乎没有任何原因无法正常工作。我建议验证这些。

也就是说,使用递归 2 被认为是不好的做法。相反,将 recursive 设置为 -1,并使用 CakePHP 惊人的 Containable Behavior 来检索任何附加信息。

另一个 "best practice" 更改是将您的发现放入模型方法而不是控制器中。所以,在你的情况下,它会是这样的:

// in the controller
$this->loadModel('DeployablesJoin');
$deployablesJoins =  $this->DeployableJoin->getAll();

//in the DeployablesJoin model
public function getAll() {
    $this->recursive = 2; // should change this to containable instead though
    return $this->find('all');
}

问题是我要加载的模型在插件中,而我没有像这样在 loadModel 中指定插件...

$this->loadModel('NodeCnc.DeployablesJoin');

我本来可以早点发现的,但是,在下面的情况下,它仍然返回结果

$this->loadModel('DeployablesJoin');

我要提交两个补丁,一个是加载没有插件部分的模型,一个是在没有插件的情况下加载模型时抛出错误,看看哪个被接受。