If I understand correctly, you want the latest DataImport record, and all of its associated ClientData records, each of which should include Hospital and Province. If that's correct, you should be calling find() on the DataImport model, which you can do because they're chained due to the association. Using Containable:
$this->set('data', $this->ClientData->DataImport->getLatest());
DatImport model:
public function getLatest() {
return $this->find(
'first',
array(
'order' => array(
$this->alias.'.created' => 'DESC'
),
'contain' => array(
'ClientData' => array(
'Hospital' => array(
'Province'
)
)
)
)
);
}
We use 'order' but 'limit' is unnecessary, as it turns out, because of 'first'. At least, I'm pretty sure I discovered that one time. If you get strange results, try adding 'limit' => 1 atthe same level as the limit & contain arrays.
On Wed, Sep 4, 2013 at 6:22 PM, Nikki McMahon <cf.mamma@gmail.com> wrote:
Hi Anja,
Thanks for replying.
I actually want all ClientData with its related data. But, on the final
level I only want the Province data and not the DataImport data, since
DataImport is associated to ClientData and so then each DataImport holds all
the ClientData associated to it as well.
Something along these lines is what I get back now if I want to get Province
data (I get it by setting recursive to a higher value)
ClientData:
ClientData[1]['id'] = 4;
ClientData[1]['name'] = 'Client Name';
ClientData[1]['DataImport']['id'] = 10;
ClientData[1]['DataImport']['ClientData'][1] = object;// excessive and
unnecessary data
ClientData[1]['DataImport']['ClientData'][2] = object;// excessive and
unnecessary data
ClientData[1]['DataImport']['ClientData'][3] = object;// excessive and
unnecessary data
ClientData[1]['DataImport']['ClientData'][4] = object;// excessive and
unnecessary data
ClientData[1]['Hospital']['id'] = 5;
ClientData[1]['Hospital']['Province']['id'] = 9;
ClientData[1]['Hospital']['Province']['name'] = 'Gauteng';
ClientData[2]['id'] = 6;
ClientData[2]['name'] = 'Client Name 2';
ClientData[2]['DataImport']['id'] = 10;
ClientData[2]['DataImport']['ClientData'][1] = object;// excessive and
unnecessary data
ClientData[2]['DataImport']['ClientData'][2] = object;// excessive and
unnecessary data
ClientData[2]['DataImport']['ClientData'][3] = object;// excessive and
unnecessary data
ClientData[2]['DataImport']['ClientData'][4] = object;// excessive and
unnecessary data
ClientData[2]['Hospital']['id'] = 8;
ClientData[2]['Hospital']['Province']['id'] = 4;
ClientData[2]['Hospital']['Province']['name'] = 'Eastern Cape';
This is what I would like it to look like - excessive ClientData removed.
Because of the level of recursion, the DataImport object loops back in on
itself, where the Hospital object expands to its logical end.
ClientData[1]['id'] = 4;
ClientData[1]['name'] = 'Client Name';
ClientData[1]['DataImport']['id'] = 10;
ClientData[1]['Hospital']['id'] = 5;
ClientData[1]['Hospital']['Province']['id'] = 9;
ClientData[1]['Hospital']['Province']['name'] = 'Gauteng';
ClientData[2]['id'] = 6;
ClientData[2]['name'] = 'Client Name 2';
ClientData[2]['DataImport']['id'] = 10;
ClientData[2]['Hospital']['id'] = 8;
ClientData[2]['Hospital']['Province']['id'] = 4;
ClientData[2]['Hospital']['Province']['name'] = 'Eastern Cape';
I'm not sure if this helps at all?
I basically loop through ClientData after pulling this, adjust some values
according to algorithms and then print it out into a table. In that table I
need to print the Province name, which is why I need to pull the province.
I am beginning to think I might just do the ugly thing and import the
hospital model and pull the name in the loop. Time constraints are a big
issue on this project.
Thanks again for replying,
Nikki
You received this message because you are subscribed to a topic in the
-----Original Message-----
From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf
Of Anja Liebermann
Sent: 04 September 2013 07:44 PM
To: cake-php@googlegroups.com
Subject: Re: Help with Model associations and find
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi Nikki,
if I undestand it correctly this might be a case for the Containable
behaviour.
But I am not sure what your fields information will do. What do you get
back, if you leave that out?
Is it correct, that you want to have all your clientData but not the
complete range of connected data?
Anja
Am 03.09.2013 22:14, schrieb Nif:
> 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(Dat
> aImport.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
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
iEYEARECAAYFAlIncUoACgkQbOdiIJzHNKHS/wCfbkLt10XB7WclFcLuHQo78N2n
21EAn1AIVhWr1Vd9BuAgyZi1vPa82ya4
=UQ/b
-----END PGP SIGNATURE-----
--
Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter
http://twitter.com/CakePHP
---
Google Groups "CakePHP" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/cake-php/d0Ffs7drkGc/unsubscribe.
To unsubscribe from this group and all its topics, 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 virus found in this message.
Checked by AVG - www.avg.com
Version: 2013.0.3392 / Virus Database: 3222/6637 - Release Date: 09/04/13
--
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.
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:
Post a Comment