CakePHP版本:2.X
我已经完全编辑了这篇文章,以便将我的问题重新表述得更清楚 我正在创建一个包含5个表的专业提案系统:
提案.........> id - name - content - created
客户..............> id - name - content - created - proposal_id
产品..........> id - name - content - created - client_id
规格..> id - name - content - created - product_id
附录.....> id - name - content - created - product_id
为了这个例子的目的,我简化了列。
所以协会如下:
提案> HasMany>客户
客户> BelongsTo>提案/客户> HasMany>产品
产品> BelongsTo>客户/产品> HasMany>规格
产品> BelongsTo>客户/产品> HasMany>附录
规格> BelongsTo>产品BelongsTo>制品
然后我用上面的关联创建了5个模型。
它看起来像这样:
提案1
....客户1
........产品1
............规格1
............规格2
............规范3
............附录1
............附录2
........产品2
............规格4
............规格5
............附录3
............附录4
提案2
....客户2
........产品3
............规范6
............规范7
............规范8
............附录5
............附录6
........产品4
............规范9
............规格10
............附录7
............附录8
等等......
我的问题是如何获得一个看起来像这样的数组,并在我的ProposalController.php操作的视图index.ctp中检索所有这些信息?
提前感谢您的帮助!
答案 0 :(得分:1)
我终于成功构建了我想要使用可包含的行为,但我遇到了一个我无法解决的问题。 我想检索提案ID并显示其附加的客户,产品,规格和附录。
为此,我在ProposalsController.php中进行了视图操作:
function admin_view($id){
$this->loadModel('Client');
// $d = $this->Proposal->find('all', array(
$d['proposals'] = $this->Proposal->find('all', array(
'conditions' => array('Proposal.id' => $id),
//'contain' => array() Tableau vide pour supprimer les liaisons
'contain' => array('Client' => array(
'fields' => array('id','name'),
'Product' => array(
'Specification', 'fields'=>array('id','name'),
'Appendice', 'fields'=>array('id','name')
)
))
));
$this->set($d);
// debug($d);
}
我正在通过其身份检索特定提案。然后我将ma查询发送到我的admin_view.ctp:
<h1>VIEW</h1>
<?php
// debug($proposals);
?>
<p>
Proposition : <strong><?php echo $proposals[0]['Proposal']['name']; ?></strong>
pour le client <strong><?php echo $proposals[0]['Client']['name']; ?></strong>
ayant le produit <strong><?php echo $proposals[0]['Client']['Product'][0]['name']; ?></strong>
avec la spec <strong><?php echo $proposals[0]['Client']['Product'][0]['Specification'][0]['name']; ?></strong>
et l'annexe <strong><?php echo $proposals[0]['Client']['Product'][0]['Appendice'][0]['name']; ?></strong>
</p>
它可以工作,但它似乎相当混乱,特别是我必须使用[0]的方式在我看来是不够的。此外,如果提案没有任何产品,也没有规格或附录,那么它会给我一个错误,因为它们当然不存在。
我如何重新排列代码以简化我的观点以使其更有意义?
以下是我的调试($ d)取消注释我的控制器的视图操作:
array(
(int) 0 => array(
'Proposal' => array(
'id' => '1',
'name' => 'Proposal 1',
'created' => '2013-02-15 00:00:00',
'modified' => '2013-02-16 03:00:47',
'due' => '2013-02-28 00:00:00',
'content' => 'Terms and conditions of the proposal 1.'
),
'Client' => array(
'id' => '1',
'name' => 'Client 1',
'Product' => array(
(int) 0 => array(
'id' => '7',
'name' => 'produit 1',
'client_id' => '1',
'Specification' => array(
(int) 0 => array(
'id' => '8',
'name' => 'spec 1 produit 1',
'value' => 'value 1 produit 1',
'product_id' => '7'
),
(int) 1 => array(
'id' => '9',
'name' => 'spec 2 produit 1',
'value' => 'value 2 produit 1',
'product_id' => '7'
)
),
'Appendice' => array(
(int) 0 => array(
'id' => '12',
'name' => 'Annexe 1 produit 1',
'content' => 'content annexe 1 produit 1',
'product_id' => '7'
)
)
),
(int) 1 => array(
'id' => '8',
'name' => 'produit 2',
'client_id' => '1',
'Specification' => array(
(int) 0 => array(
'id' => '10',
'name' => 'spec 1 produit 2',
'value' => 'value 1 produit 2',
'product_id' => '8'
),
(int) 1 => array(
'id' => '11',
'name' => 'spec 2 produit 2',
'value' => 'value 2 produit 2',
'product_id' => '8'
),
(int) 2 => array(
'id' => '12',
'name' => 'spec 3 produit 2',
'value' => 'value 3 produit 2',
'product_id' => '8'
)
),
'Appendice' => array(
(int) 0 => array(
'id' => '13',
'name' => 'Annexe 1 produit 2',
'content' => 'content annexe 1 produit 2',
'product_id' => '8'
)
)
)
)
)
),
(int) 1 => array(
'Proposal' => array(
'id' => '1',
'name' => 'Proposal 1',
'created' => '2013-02-15 00:00:00',
'modified' => '2013-02-16 03:00:47',
'due' => '2013-02-28 00:00:00',
'content' => 'Terms and conditions of the proposal 1.'
),
'Client' => array(
'id' => '2',
'name' => 'Client 2',
'Product' => array()
)
)
)
我得到了我需要的意思,意思是ID为1的提案,但是下面是另一个数组,显示我不需要的2个模型Proposal和Client的关联,因为我已经在第一个数组(int)0中有了它。
我做错了什么?
非常感谢你的帮助!