Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to reproduce this query with the Orm package
  • Hello, I tried many ways to achieve this using the ORM and I wasn't able to do it. 2 problems I had is :
    - Orm\Query->select() escapes my \Db::expr() objects as if it was a standard field (string)
    - Orm\Query has no group_by() method
            $t = \Db::select(\Db::expr('COUNT(comm_id) AS count_result'), 'comm_parent_id')
                    ->from('cms_comments')
                    ->and_where('comm_type', '=', 'blog')
                    ->and_where('comm_parent_id', 'in', $ids)
                    ->group_by('comm_parent_id')
                    ->execute()->as_array();
    

    Here is what I was hoping to do (non functionnal) :
            $t = \Cms\Model_Comment::query()
                    ->select(\Db::expr('COUNT(comm_id) AS count_result'), 'comm_parent_id') // Db::expr() works badly
                    ->where(array(
                        array('comm_type', '=', 'blog'),
                        array('comm_parent_id', 'in', $ids),
                    ))
                    //->group_by('comm_parent_id') // No group_by() method
                    ->get();
    

    Any help appreciated, thanks in advance for that!
  • Try this:
    $t = \Cms\Model_Comment::query() ->select(array(\Db::expr('COUNT(comm_id)'), 'count_result'), 'comm_parent_id')
    
  • No luck. The array() construction doesn't work with Orm\Query->select() as it's a simple foreach (func_get_args() as $field) The second argument to Db::expr() makes it better, but I suspect it's just ignored, that's why. Here is the error I get:
    Fuel\Core\Database_Exception Object ( [message:protected] => Unknown column 't0.COUNT(comm_id)' in 'field list' [ SELECT `t0`.`COUNT(comm_id)` AS `t0_c0`, `t0`.`comm_parent_id` AS `t0_c1` FROM `cms_comments` AS `t0` WHERE `t0`.`comm_type` = 'blog' AND `t0`.`comm_parent_id` IN ('19', '18', '17', '15', '14', '13', '12', '11', '9', '10', '7', '6', '5', '2', '4', '1', '3') ]
    

    That's for the select() part. Still doesn't solve the group_by() part. I think it's not possible for now. My main concern when using Db methods was the
    ->from('cms_comments')
    
    in raw. I just put
    ->from(call_user_func('\Cms\Model_Comment::table'))
    
    to link it with the model. Works fine.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion