Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
subquery in select orm
  • Hi guys,

    can someone please tell me how to use a subquery in a select() statement in ORM/Query builder?
    In the where() function I can make it work... But in select it doesn't work...

    $subquery = \Model_User_Log::query()->select(\DB::expr('COUNT(*) AS counter'))->where('user_log.user_id','user.id');

    $query = \Model_User::query()->select($subquery->get_query());
  • get_query() returns the SQL as a string, so that will never work. You should be able to just pass the query itself:

    $query = \Model_User::query()->select($subquery);
  • Nope that also doesn't seem to work...
  • Looked at the code, and indeed, you can't do that.

    You have to take into account that an ORM is not a query builder. It's make to allow object oriented access to your database.

    What exactly are you trying to do here? Count the number of log-entries per user?  This is very easily done with a standard DB query.

Howdy, Stranger!

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

In this Discussion