Hello.
I would like to use Yii to create JOIN and order data by column in joined table.
I have 1 table with Primary Key and Foreign Key. Let’s say it is table “user2post”.
PK = post_id, user_id
FK = user_id
Second table has only PK. It’s table “user”.
PK = user_id
column_1 = username
I need to get everything that’s in table “user2post” and add user names. Normal query would look like this:
SELECT user2post.post_id, user.name
FROM user2post
INNER JOIN user
ON user2post.user_id = user.user_id
ORDER BY user.name
Now I try to do this using Yii:
In Yii I added relation from "user2post" to "user" table. It works.
'getUser' => array(self::BELONGS_TO,'user','user_id')
Now I use it like this:
$criteria=new CDbCriteria;
$criteria->together = 'getUser';
(I know that I can use "with" but in my case it does not work)
user2post::model()->findAll($criteria);
It works.
… and I would like to add
$criteria->order = 'user.name';
But it does not work. I get this error:
– The column prefix ‘user’ does not match with a table name or alias name used in the query.
"user" is name of table and name of model.
Can you help please?
And doesn’t exist any tutorial that would explain problems like this? I know that there is this page:
http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-options
But it does not explain anything important. Just obvious things