Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to count results of an ORM query with related fields
  • I have a problem with this query :

    $q = Model_Rubrique::query()
    ->related('actualite')
    ->where('actualite.title', 'LIKE', '%'.$str.'%');
    $nb = $q->count();


    I read that this is not possible to count query results in such a situation, so is there another way to do it ?
    I thought about a hack : using $q->get_query() and replacing the text before the first FROM by SELECT count(*), but this is not very elegant...

    In fact I need it in conjunction with a recursive function extending the Models which may add many related fields and I do not want to make it in a dirty way.
  • HarroHarro
    Accepted Answer
    I don't see why it is not possible. Have you tried it? And what was wrong with the result?
  • I'm embarrassed, there is no more error... I think this was my override of the _init() in my models, or the uncompleted relations... I remember the error was in fuel/packages/orm/classes/query.php line 839, $columns was not an array but an object. Sorry.
  • No problem, good to see you have it sorted.
  • I think I have same error as leMathou' one..
    But I have no idea to fix this.

    now I have the error below.

    Argument 2 passed to call_fuel_func_array() must be of the type array, object given, called in ...\fuel\packages\orm\classes\query.php on line 1144

    Can you share what the error was, if you remember...

    #1666
    // Get the columns
    $columns = \DB::expr('COUNT('.($distinct ? 'DISTINCT ' : '').
    \Database_Connection::instance($this->connection)->quote_identifier($select).
    ') AS count_result');

    // Remove the current select and
    $query = \DB::select($columns);

    // Set from view or table
    $query->from(array($this->_table(), $this->alias));

    $tmp = $this->build_query($query, $columns, 'count');
    $query = $tmp['query'];
    $count = $query->execute($this->connection)->get('count_result');
    $columns is Database_Expression class, not array..
    and the second parameter of build_query method expected array...
    I don't understand...

    any help will be great.
    thank.
  • oh this post was 2013... 

  • for someone like me,
    You may need to use rows_limit and rows_offset instead of limit and offset after count.

    $count = $query->count();
    $list = $query->rows_limit(100)->rows_offset(100 * ($page-1))->get();

Howdy, Stranger!

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

In this Discussion