簡體   English   中英

CakePHP遞歸不適用於loadModel和this-> set

[英]CakePHP Recursive not working with loadModel and this->set

當我使用$ this-> loadModel時,遞歸似乎不起作用,我想說我還記得在這種情況下必須顯式設置belongsTo和hasMany的一些知識,但這似乎是抗cakephp-auto-magical的方法,我還沒有查找說明如何使用的答案

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

除了關聯設置不正確外,似乎沒有任何原因可以使您的“遞歸”功能無法正常工作。 我建議驗證一下。

話雖如此,使用遞歸2被認為是不好的做法。 取而代之的是,將遞歸設置為-1 ,並使用CakePHP的驚人的Containable Behavior來檢索任何其他信息。

另一個“最佳實踐”更改是將您的發現放入模型方法中,而不是放入控制器中。 因此,在您的情況下,將是這樣的:

// 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');

我將提交兩個補丁,一個補丁用於在沒有插件部分的情況下加載模型,一個補丁在沒有插件的情況下拋出錯誤,看看哪個被接受。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM