On Sat, Feb 19, 2011 at 12:35 PM, tofu <[email protected]> wrote:
> I have vendors, each vendor has many reviews and many categories. I'd
> like paginate to be able to sort by the AVG reviews and also
> categories. For example, this displays ok, but only the vendor name is
> sortable:
>
> // in vendor controller
> $this->paginate = array(
>        'limit'   => 20,
>        'fields' => array('Vendor.name', 'Category.name', 'AVG
> (VendorReview.rating) as avg_rating'),
>        'group'  => array('VendorCategory.id'),
>        'joins'  => array(
>            array(
>                'table'=>'vendor_reviews',
>                'alias'=>'VendorReview',
>                'type' => 'LEFT',
>
> 'conditions'=>array('Vendor.id=VendorReview.vendor_id')
>            ),
>            array(
>                'table'=>'categories',
>                'alias'=>'Category',
>                'type' => 'LEFT',
>
> 'conditions'=>array('VendorCategory.category_id=Category.id')
>            )
>      ),
>      'conditions' =>$conditions
> );
>
> $vendors = $this->paginate('Vendor');
> $this->set(compact('vendors'));
>
>
>
> <!--in vendor view-->
> ...
> <th><?php echo $this->Paginator->sort('avg_rating');?></th>
> <th><?php echo $this->Paginator->sort('name');?></th>
> <th><?php echo $this->Paginator->sort('category');?></th>
> ...

Try  $this->Paginator->sort('VendorCategory.category_id');

For the avg, I think it might be due to the way Cake returns
calculated fields. Instead of putting them under the ModelName key,
the key is numeric (eg. 0). Have a look at this page:

http://nuts-and-bolts-of-cakephp.com/2008/09/29/dealing-with-calculated-fields-in-cakephps-find/

Although I'm unsure this will help, actually. I don't know that
passing "avg_rating" to sort() is sufficient. Worth a try, though.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to