Yii Framework Forum: Relations in complex queries - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relations in complex queries Rate Topic: -----

#1 User is offline   andser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 12-February 10

Posted 14 July 2010 - 02:15 AM

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?
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 July 2010 - 03:31 AM

Try

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

0

#3 User is offline   andser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 12-February 10

Posted 14 July 2010 - 03:38 AM

View PostMike, on 14 July 2010 - 03:31 AM, said:

Try

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


Column not found: 1054 Unknown column 'ips.ip' in 'where clause'
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 July 2010 - 03:45 AM

Oh, you also need to query like this:

$payedaccs = Accounts::model()->with('user.ips')->findAll($criteria);

0

#5 User is offline   andser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 12-February 10

Posted 14 July 2010 - 03:48 AM

View PostMike, on 14 July 2010 - 03:45 AM, said:

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

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 July 2010 - 04:19 AM

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

#7 User is offline   andser 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 12-February 10

Posted 14 July 2010 - 05:28 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users