CakePHP递归不使用loadModel和this-> set

时间:2015-01-10 15:01:42

标签: php cakephp recursion cakephp-model

当我使用$ this-> loadModel时,递归似乎不起作用,我想说我记得在这种情况下必须明确设置belongsTo和hasMany,但这似乎是反蛋糕 - 自动 - 魔法,我还没有找到任何答案说明如何使用

$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);

编辑:下面的完整代码

My DeployablesJoin Model

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
        (
        )

2 个答案:

答案 0 :(得分:1)

似乎没有任何理由是您的递归'除了错误设置的关联之外,它不会起作用。我建议验证这些。

话虽如此,使用递归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');
}

答案 1 :(得分:1)

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

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

我之前会抓住这个,但是,以下内容仍然会返回结果

$this->loadModel('DeployablesJoin');

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