当我
使用 $this->loadModel 时,递归似乎不起作用,我想说我记得在这种情况下必须显式设置归属和 hasMany,但这似乎是反 cakephp-auto-magic 的,我还没有找到任何说明如何使用的答案
$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);
编辑:下面的完整代码
我的可部署项联接模型
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/模具的结果
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/模具的结果
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/死亡时,我得到以下结果
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
(
)
除了错误设置的关联之外,似乎没有任何原因您的"递归"不起作用。 我建议验证这些。
话虽如此,使用递归 2 被认为是不好的做法。 相反,将递归设置为-1
,并使用CakePHP惊人的可包含行为来检索任何其他信息。
另一个"最佳实践"更改是将发现放入模型方法中,而不是在控制器中。 因此,在您的情况下,它将是这样的:
// 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');
我将提交两个补丁,一个用于加载没有插件部分的模型,另一个用于在没有插件的情况下加载时抛出错误,看看哪个被接受。