Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Alias for related table field
  • Has two tables: users (belongs to) and positions (has many)

    I make the jqGrid table on the page and give json data from that query:

    $users = Model_Users::query()
        ->related('position')
        ->select('id', 'username', 'email', 'firstname', 'lastname')
        ->order_by($sidx, $sord)
        ->rows_limit($pager->per_page)
        ->rows_offset($pager->offset)
        ->get();

    Here I don't select any data for positon, but when I prepare data for json answer I do next:

    foreach ($users as $id => $user)
                $result['rows'][] = array(
                    'id' => $id,
                    'cell' => array(
                        'id' => $id,
                        'username' => $user->username,
                        'email' => $user->email,
                        'firstname' => $user->firstname,
                        'lastname' => $user->lastname,
                        'position' => $user->position->name
                    )
                );

    It's ok, but when I try to order that column (position), I get the error in SQL query.

    I need to set alias for positions.name as position or any other name for order by clause.
    How can I do this?
    Thank you.
  • I find the solution of this problem with setting for the position column in jqGrid as 't1.name' and appropriate name in the json respond. But it is not beautiful solution, is it?
  • HarroHarro
    Accepted Answer
    You use order_by('relation-name.column', 'order'), so in this case order_by('position.name', 'asc'), assuming that model has a column called 'name'.

    You don't give the exact error, so it is a bit guesswork as to the exact problem.
  • Thank you, Harro.
    Your advice is useful.

Howdy, Stranger!

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

In this Discussion