I finally succeeded in building what i wanted using containable behaviour but i got a problem that i can't solve.
I'd like to retrieve a proposal id and display its attached clients, products, specifications and appendices.
To do so, i made the view action in my ProposalsController.php like this:
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);
}
I'm retrieving the particular proposal via its id in my condition. Then I send ma query to my 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>
It works but it seems to be quite a mess especially the way i have to use [0] that seems to me non adequate. Also, if a proposal hasn't any product nor specifications or appendices, it gives me an error for those because they don't exist of course.
How could i rearrange my code to simplify my view to get it to make more sense?
Also the following is my debug($d) uncommented from the view action of my controller :
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()
)
)
)
I get what i need meaning the proposal with id 1 but below there's another array displaying the association of my 2 models Proposal and Client that I don't need because I already have it in the very first array(int) 0.
What am i doing wrong?
Thanks a lot for your help!