Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm
Alias for related table field
bvn
April 2015
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.
bvn
April 2015
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?
Harro
April 2015
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.
bvn
April 2015
Thank you, Harro.
Your advice is useful.
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,089
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
261
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
bvn
April 2015
Harro
April 2015