andser
(Funcod3r)
July 14, 2010, 7:15am
1
Hello!
I have 3 models. First :
class Accounts extends CActiveRecord
{
// ....
public function relations()
{
return array(
'user'=>array(self::BELONGS_TO, 'User', 'user_id',
'with'=>array('ips')
),
);
}
Second :
class User extends CActiveRecord
{
// ...
public function relations()
{
return array(
'ips'=>array(self::HAS_MANY, 'UsersIP', 'user_id', 'order'=>'ips.timestamp DESC'),
);
}
And third :
class UsersIP extends CActiveRecord
{
/**
* @var integer $id
* @var string $ip
* @var integer $timestamp
* @var integer $user_id
*/
// ...
}
I wrote this code :
$criteria = new CDbCriteria;
$criteria->condition = "user.ips.ip = '127.0.0.1'";
$payedaccs = Accounts::model()->with('user')->findAll($criteria);
But it didn’t work . Column not found: 1054 Unknown column ‘user.ips.ip’ in ‘where clause’.
How to get "ip" column in this condition?
mikl
(Mike)
July 14, 2010, 8:31am
2
Try
$criteria->condition = "ips.ip = '127.0.0.1'";
andser
(Funcod3r)
July 14, 2010, 8:38am
3
Column not found: 1054 Unknown column ‘ips.ip’ in ‘where clause’
mikl
(Mike)
July 14, 2010, 8:45am
4
Oh, you also need to query like this:
$payedaccs = Accounts::model()->with('user.ips')->findAll($criteria);
andser
(Funcod3r)
July 14, 2010, 8:48am
5
Oh, you also need to query like this:
$payedaccs = Accounts::model()->with('user.ips')->findAll($criteria);
$criteria->condition = "ips.ip = '127.0.0.1'";
$payedaccs = Accounts::model()->with('user', 'user.ips')->findAll($criteria);
The same error
mikl
(Mike)
July 14, 2010, 9:19am
6
Can you check the created SQL? (Configure a CWebLogRoute, set YII_DEBUG true and don’t use yiilite.php to see the SQL on the bottom of your page)
andser
(Funcod3r)
July 14, 2010, 10:28am
7
There was no join with UsersIP table. So I added together() to call and it’s worked. There’s the solution :
$payedaccs = Accounts::model()->with('user', 'user.ips')->together()->findAll($criteria);
Thanks, Mike