Wednesday, September 29, 2010

Mysql GROUP in containable not working

Hey all,

Trying to use GROUP by in containable behaviour and it's not working as expected.

Ratings model has fields: Rating.id, Rating.restaurant_id, Rating.rating (1-5)

I have a query (which amongst other things) fetches the average + total ratings for a restaurant:

$restaurant = $this->Restaurant->find('first', array(
    'fields' => array(Restaurant.x, Restaurant.y, Restaurant.z),
    'contain' => array(
        'model1' => array(x, y, z),
        'Rating' => array(
          'fields' => array(
            'AVG(Rating.rating)',
            'COUNT(Rating.id)'
          )
        ),
        'model2' => ...
    )
));

It works fine on my mysql version 5.0 on my dev server but as soon as I upload it onto live site mysql 4.1 I get error saying I cannot mix group columns (avg, sum, etc) is illegal without a GROUPBY field.

So ok fair play, I'm not including a groupby field. I jumped in and added one:

$restaurants = $this->Restaurant->find('all', array(
    'fields' => array(Restaurant.x, Restaurant.y, Restaurant.z),
    'contain' => array(
        'model1' => array(x, y, z),
        'Rating' => array(
          'fields' => array(
            'AVG(Rating.rating)',
            'COUNT(Rating.id)'
          ),
          'group' => 'Restaurant.id'       // I also tried 'group' => 'id'
        ),
        'model2' => ...
    )
));



Which I would figure works but it doesn't. I get an error saying that there is no column called 'group' and it is trying to execute this query: 

SELECT `Rating`.`restaurant_id`, AVG(`Rating`.`rating`), COUNT(`Rating`.`id`), `Rating`.`group` FROM `ratings` AS `Rating` WHERE `Rating`.`restaurant_id` = (5)

Am I using containable incorrectly?



Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
 
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: