Sunday, August 18, 2013

Re: Include count of nested associated object as virtual field.

Hi,

I have been able to use the counter cache for computing the number of questions in a chapter since the Question model belongs to the Chapter model.  But now I want to calculate the total number of questions in a Unit where the Chapter model belongs to the Unit Model.  Can this also be done with counterCache?  Ideally I would like to do this with virtualFields so that this data is returned everytime a Unit object is loaded in my application.  

Thanks.

On Sunday, August 18, 2013 4:52:51 AM UTC-4, Nicholas Amon wrote:
Hi,

Thanks for the reply.  Would that require me to introduce the field in the database schema?  I would like to also include the count in chapters, units and subjects.  Would that require a schema change for each of these tables?

Thanks again for your reply.

On Sunday, August 18, 2013 2:14:49 AM UTC-4, Jeremy Burns wrote:
counterCache is your friend.

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 17 Aug 2013, at 23:02:51, Nicholas Amon <nichol...@gmail.com> wrote:

Hi,

I have the following model relationships

Question belongs to Chapter
Chapter belongs to Unit

I want to add a virtual field in the Unit model that counts how many questions a unit has.  I tried this:

public $virtualFields = array(
"questionCount" => 'SELECT COUNT(*) FROM questions as Question JOIN chapters as Chapter ON (Question.chapter_id = Chapter.id) JOIN units as Unit ON (Chapter.unit_id = Unit.id)'
);

The problem here is that it returns the same value for all untis.  The issue here is that I want the count for each Unit instance.  In other words, I would like to add a where clause so that the query becomes something similar to:

public $virtualFields = array(
"questionCount" => 'SELECT COUNT(*) FROM questions as Question JOIN chapters as Chapter ON (Question.chapter_id = Chapter.id) JOIN units as Unit ON (Chapter.unit_id = Unit.id)  WHERE Unit.id = CAKE_REPLACE_WITH_CURRENT_ID'
);

How do I get cake to CAKE_REPLACE_WITH_CURRENT_ID to be the ID of the current Unit instance that is being retrieved?

I hope I have made my question clear.  Thanks in advance.

--
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+u...@googlegroups.com.
To post to this group, send email to cake...@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: