2011-03-18 26 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