Relations in complex queries

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?

Try


$criteria->condition = "ips.ip = '127.0.0.1'";

Column not found: 1054 Unknown column ‘ips.ip’ in ‘where clause’

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 :(

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)

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 ;)