Wednesday, November 19, 2014

Re: hasMany and find()

Thank you again John.

I just can't see how it could not be done more easily. 
I have 1 million records for Parent model.
That sub-query could take more time than users are willing to wait.

I can write SQL like this for example

SELECT p.id, p.name, c.name FROM parents p
LEFT JOIN  children c ON p.id = c.parent_id
WHERE c.name IS NULL;

How do I make CakePHP do something like this?




2014年11月18日火曜日 3時38分31秒 UTC+9 John Andersen:
Ok, if you cannot modify the database tables, then the other option is to use sub-queries, see the CakePHP book at:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries

My understanding of this, is that you first construct the SQL statement for finding all the parent ids in the child table, then uses that SQL statement as a condition for finding all parent records with the id not being found by the previous SQL statement.

Hope that helps you out :)
Enjoy, John

On Monday, 17 November 2014 06:05:48 UTC+2, Kento Furui wrote:
Thank you John.

counterCache seems to be a good idea. 
But there must be a way to do this without adding a column to an existing table.
[snip]

--
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/d/optout.

No comments: