1

I'm trying to order by and group by tag

$result = Model_Tag::query()->select(\Fuel\Core\Db::expr('count(*)'),'count')->select('tag')->group_by('tag')->order_by('count','desc')->get();

$result = Model_Tag::query()->select(\Fuel\Core\Db::expr('count(*)','count'))->select('tag')->group_by('tag')->order_by('count','desc')->get();

However, no matter what I do I get the error that the count is not defined as ORM insists on aliasing the field name:

SELECT count(*) AS t0_c0, t0.tag AS t0_c1, t0.id AS t0_c2 FROM tag AS t0 GROUP BY t0.tag ORDER BY t0.count DESC"

Resulting in a column not found error

Alternatively:

$query = Model_Tag::query()->select(\Fuel\Core\Db::expr('count(*) as count'))->select('tag')->group_by('tag')->order_by('count','desc')->get();

query

SELECT count(*) as count AS t0_c0, t0.tag AS t0_c1, t0.id AS t0_c2 FROM tag AS t0 GROUP BY t0.tag ORDER BY t0.count DESC

gives a syntax error

paullb
  • 4,293
  • 6
  • 37
  • 65

1 Answers1

0

In FuelPHP Version 1.8, access you controller and you query set this example:

$result = Model_Tag::query()->group_by('tag')->order_by('count','desc')->get();

if you get the count

$count = count($result);

Warning! Do not call your select('tag') because you are already using Model_Tag

Ref: https://fuelphp.com/docs/classes/database/usage.html

Darlan Dieterich
  • 2,369
  • 1
  • 27
  • 37