0
votes

CakePHP version : 2.X

I have completely edited this post in order to reformulate my question to be clearer.
I'm creating a professional proposal system which contains 5 tables:

proposals.........> id - name - content - created
clients..............> id - name - content - created - proposal_id
products..........> id - name - content - created - client_id
specifications..> id - name - content - created - product_id
appendices.....> id - name - content - created - product_id

I've simplified the columns for the purpose of this example.

So the associations are the following:

proposals > HasMany > clients
clients > BelongsTo > proposals / clients > HasMany > products
products > BelongsTo > clients / products > HasMany > specifications
products > BelongsTo > clients / products > HasMany > appendices
specifications > BelongsTo > products BelongsTo > products

Then I created 5 models with the associations above.

It would look something like this:

proposal 1
....client 1
........product 1
............specification 1
............specification 2
............specification 3
............appendice 1
............appendice 2
........product 2
............specification 4
............specification 5
............appendice 3
............appendice 4

proposal 2
....client 2
........product 3
............specification 6
............specification 7
............specification 8
............appendice 5
............appendice 6
........product 4
............specification 9
............specification 10
............appendice 7
............appendice 8

And so on...

My question is how can I get an array that looks like this and retrieve me all these information in the view index.ctp of my ProposalController.php action?

Thank a lot in advance for your help!

1
You should really read up on how to bake in the console. It will automate all of your relations in your models for you. This is a bit dated but still effective.. I recommend youtube.com/playlist?list=PL9B2E2E37CCB661D6 Andrew Perkins tutorials, mainly Cakephp Blog Tutorial Part 10 - Setting Up the Cake Console and BakeTim Joyce
Thank you LetterSticker, some of these day I will try that but at the moment I'm learning OOP and CakePHP framework so I still prefer to get my hand dirty so I know how it works. It's a bit of a headache I have to admit but I assume that once you know how it works you can totally enjoy the full power of this framework.Julian Livin' in China
Does anybody could lead me in the right direction, i'm really stuck here? I can't figure out how to exploit them in order to display an array with all the informations the tables contain according to theirs linking. Thank you very much in advance, i really appreciate it.Julian Livin' in China

1 Answers

1
votes

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!