2011-03-18 49 views
0

我對automagic及相關型號數據有疑問。我有4個模型:Exercice,Ecriture,Ligne,Compte。 Exercice hasmany Ecriture和Ecriture hasmany Ligne和Compte hasmany Ligne有兩種不同的外鍵關係。我想用automagic填充我的表單。因此,使用$這 - >數據,我給這個數組的觀點:Automagic及相關型號數據

Array 

(

[Exercice] => Array 

(

[id] => 1 

[theme] => marchandises 

) 

[Ecriture] => Array 

(

[0] => Array 

(

[id] => 1 

[exercice_id] => 1 

[numero] => 1 

[enonce] => Quelle est la dincee? 

[Ligne] => Array 

(

[0] => Array 

(

[id] => 1 

[ecriture_id] => 1 

[compte_debit_id] => 2 

[compte_credit_id] => 1 

[montant_debit] => 23 

[montant_credit] => 23 

[libelle] => Achat de marchandises 

[student_id] => 1 

[CompteDebit] => Array 

(

[id] => 2 

[nom] => achat marchandises 

) 

[CompteCredit] => Array 

(

[id] => 1 

[nom] => caisse 

) 

) 

) 

) 

現在,如果我想獲得第一級我簡單地使用:

$這個 - >形式 - >輸入( 'Ecriture.0.enonce');

一切正常!

但是用我無法訪問級別Seconde系列:

$這個 - >形式 - >輸入( 'Ecriture.0.Ligne.0.libelle');

這是爲什麼?有人能幫助我嗎?

+0

你的問題不清楚,問題是什麼? – Leo 2011-03-18 00:56:22

+0

對不起,問題實際上是在列表框中。我現在改變了。我的問題是爲什麼相對於第二級的字段沒有自動填充? – Vestrep 2011-03-18 10:08:36

回答

0

嘗試以下操作:

在控制器:

$this->data = array( 
'Exercice' => array('column1' => 'value1', 'column2' => 'value2', etc.), 
'Ecriture' => array(0 => array('column1' => 'value1', 'column2' => 'value2', etc.)), 
'Ligne' => array(0 => array('column1' => 'value1', 'column2' => 'value2', etc.)), 
) 

正如你所看到的,就是我建議是把對關聯模型中的數據在同級別和NOT嵌套在一個其他。因此,通過這種方法,您可以設置表單元素,如:$this->Form->input('Exercice.column1');$this->Form->input('Ecriture.0.column1');

順便說一句,現在我想想,你應該能夠通過使用自動(表單字段)填充$this->data陣列read(),如:$this->data = $this->ModelName->read(null, $recordId)。查看更多here in the cookbook

+0

其實,我使用read和containable來接受所有關係的層次。但由於某些原因,Cake僅填充第一級的字段。 – Vestrep 2011-03-18 10:04:32

+0

然後嘗試重新排列字段,以便所有字段都處於第一級。 – YOMorales 2011-03-18 18:16:39