Retrieve Associated Count

2009-03-17 Thread Tony Thomas

I have a simple hasMany relationship set up with CakePHP and I want to
retrieve a couple of fields from the parent table based on the count
of the child.

The scenario: I have boxes that hold 81 aliquots each. (Each box is
9X9.) I want to retrieve the id and description from the boxes table
of boxes that have fewer than 81 aliquots to generate a list. In other
words, I want a list of boxes that are not full.

My models:

class Box extends AppModel {
var $name = 'Box';
var $hasMany = array('Aliquot');
var $belongsTo = 'Freezer';
}

class Aliquot extends AppModel
{
var $name = 'Aliquot';
var $belongsTo = array('Box','Specimen');
}

I can't seem to work out the best way to get a count of the aliquots
for each box. I haven't been working with CakePHP all that long, so
I'm hoping there's a relatively simple way to do this that I'm
missing. It doesn't seem like a very complicated thing to do, yet I've
spent the better part of my morning working on it with not success.
This seems like it would work:

http://book.cakephp.org/view/75/Saving-Your-Data#counterCache-Cache-your-count-490

Except that my tables are already populated, so creating the
aliquot_count field in my boxes table just leaves me with a bunch of
empty fields. Does anyone have some advice that will point me in the
right direction?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Retrieve Associated Count

2009-03-17 Thread Xoubaman

The counterCache field is updated with each insert or delete (maybe
more often), so if your boxes are changing allt he time, it will not
be a problem.

If it isn't the case, you can fetch all the boxes and their count and
take only the count81 using a foreach loop.

On Mar 17, 5:17 pm, Tony Thomas truet...@gmail.com wrote:
 I have a simple hasMany relationship set up with CakePHP and I want to
 retrieve a couple of fields from the parent table based on the count
 of the child.

 The scenario: I have boxes that hold 81 aliquots each. (Each box is
 9X9.) I want to retrieve the id and description from the boxes table
 of boxes that have fewer than 81 aliquots to generate a list. In other
 words, I want a list of boxes that are not full.

 My models:

 class Box extends AppModel {
     var $name = 'Box';
     var $hasMany = array('Aliquot');
     var $belongsTo = 'Freezer';

 }

 class Aliquot extends AppModel
 {
     var $name = 'Aliquot';
     var $belongsTo = array('Box','Specimen');

 }

 I can't seem to work out the best way to get a count of the aliquots
 for each box. I haven't been working with CakePHP all that long, so
 I'm hoping there's a relatively simple way to do this that I'm
 missing. It doesn't seem like a very complicated thing to do, yet I've
 spent the better part of my morning working on it with not success.
 This seems like it would work:

 http://book.cakephp.org/view/75/Saving-Your-Data#counterCache-Cache-y...

 Except that my tables are already populated, so creating the
 aliquot_count field in my boxes table just leaves me with a bunch of
 empty fields. Does anyone have some advice that will point me in the
 right direction?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---