You don't need a custom select.

controller:

$this->Category->bindModel(array(
  'hasOne' => array(
    'ProductCount' => array(
      'className' => 'Product',
      'fields' => 'COUNT(ProductCount.id) AS num_products'
    )
  )
));
$this->set('categories', $this->Category->find('all', array(
  'conditions' => '...',
  'recursive' => 0, // at least 0
  'group' => 'Category.id'
));

// view

echo '<ul>';
foreach($categories as $cat) {
  echo '<li>' . $cat['Category']['title'] . ' [' . $cat[0]
['num_products'] . ']' . '</li>';
}
echo '</ul>';

On Nov 20, 12:24 am, aron <[EMAIL PROTECTED]> wrote:
> yes. that could work. How would I echo that from the array in the
> view?
>
> On Nov 19, 5:15 pm, brian <[EMAIL PROTECTED]> wrote:
>
> > What else are you using $colors for?  I had to do something quite similar.
> > You might want to use a custom SELECT along the lines of:
>
> > SELECT c.name, COUNT(cp.product_id) AS total FROM colors AS c
> > LEFT JOIN colors_products AS cp ON cp.color_id = c.id
> > GROUP BY c.name;
>
> > On Wed, Nov 19, 2008 at 6:33 PM, aron <[EMAIL PROTECTED]> wrote:
>
> > > Hi Everyone,
>
> > > I am working on a project where we need to display the number of
> > > listings in each categories .
>
> > > .ie
>
> > > blue (34)
> > > green (20)
> > > red (12)
> > > orange (3)
>
> > > where the number in the brackets is the number of products in each
> > > color. ie. 34 products are blue. Click blue to filter products.
>
> > > What would be the correct way of doing this with a MVC model? I am
> > > currently using a request action call in the loop but don't want to do
> > > it this way b/c it will hog resources.
>
> > > below is my view code for the color selector
>
> > > <?php foreach ($colors as $color): ?>
>
> > >                        <?php echo
> > > $html->link($color['color']['type'],'/products/price/' .
> > > $color['color']['id']);  ?>
>
> > >                         (<?php echo $products =
> > > $this->requestAction('/products/
> > > filtercount/'  . $color['color']['id'],array('return'));?> )
>
> > > <?php endforeach; ?>
>
> > > this works but is not the correct way. I was thinking of creating a
> > > custom global function that will give me a count.
> > > Or can I create a helper with a database call?
> > > any ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to