Tuesday, September 3, 2013

Help with Model associations and find

Hi All,

I have something I am struggling with and hoping someone can help? I've used Cake for some simple tools and site structure in the past and I'm currently working on my first complex db design within the framework.

I know how to do this in mySQL, but cannot figure out how to use Cake's Model associations and find method to get it done. - All associations are set up in the Models already.

I have a table 'ClientData'. It has foreign keys to 2 other tables, Hospital and DataImport. This is a manyToOne relationship (I think - basically, many ClientData's can have one Hospital and DataImport).
Hospital then has a foreign key to the Province table (Many Hospitals can have one Province).

so:
[1]['id'] = 4;
[1]['name'] = 'Client Name';
[1]['DataImport']['id'] = 10;
[1]['Hospital']['id'] = 5;
[1]['Hospital']['Province']['id'] = 9;

That's the basic layout (minus lots of the actual data) where [i] is the ClientData row and the result will have many ClientData rows.

I want to select all of the ClientData's that are associated to the latest created DataImport and get back The ClientData with the Hospital data associated to the ClientData and then subsequently the Province data associated to the Hospital - Much like the structure above.

I am using this code:
$latestData = $this->ClientData->find('all',array('fields'=>array('MAX(DataImport.created)','*'),'recursive' => 1));
But all I receive is one ClientData row and the MAX created field. If I increase recursive to 3 then I get 1 ClientData row with associated data alongside (Hospital and DataImport) and then inside the DataImport row are all the ClientDatas I want and under those are DataImport data and Hospital Data.

This is part of the way correct, however, I don't feel it is right... seems odd to have that top level return of one ClientData and it's associations and then have the rest under the DataImport associated to that one ClientData, all with their own DataImport data.

Also, I still don't have the Province data I want. If I increase recursive to 4 then I get teh hospital data but also the very sub level DataImport under each ClientData brings back all the ClientData once again.

I am sure there must be a better way to do this, but I just cannot find out how.
I can import the Hospital Model and then create a function to pull it's data which will bring the Province data associated, but this just isn't good practice and I really want to use the Model associations properly.

Sorry, I know this is long winded and possibly not well described. I hope it makes sense.
Any advice or help would be very much appreciated.
Thanks in advance,
Nikki

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: