Yii Framework Forum: Condition involving a not-directly related table in CActiveDataProvider - Yii Framework Forum

Jump to content

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

Condition involving a not-directly related table in CActiveDataProvider Rate Topic: -----

#1 User is offline   transistor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 78
  • Joined: 05-June 09
  • Location:Mexico

Posted 28 February 2010 - 04:11 AM

I have the following tables:
Posted Image
I need to select items from table3 with a condition involving table1.
$condition = 'table1.someField = someValue';
$anuncios = new CActiveDataProvider('anuncio', array(
	'criteria'=>array(
		'condition'=>$condition,
		// I am using table 2 also, so:
		'with'=>'table2',
		'together'=>true,
	),
	'pagination'=>array(
		'pageSize'=>self::PAGE_SIZE,
	),
	'sort'=>$sort,
));

Of course, this doesn't work, I get an error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'table1.someField' in 'where clause'
Can you help me?
What am I missing here?
Thanks
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 744
  • Joined: 02-February 09

Posted 28 February 2010 - 04:22 AM

View Posttransistor, on 28 February 2010 - 04:11 AM, said:

I need to select items from table3 with a condition involving table1.
$condition = 'table1.someField = someValue';
$anuncios = new CActiveDataProvider('anuncio', array(
	'criteria'=>array(
		'condition'=>$condition,
		// I am using table 2 also, so:
		'with'=>'table2',
		'together'=>true,
	),
	'pagination'=>array(
		'pageSize'=>self::PAGE_SIZE,
	),
	'sort'=>$sort,
));

Of course, this doesn't work, I get an error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'table1.someField' in 'where clause'
Can you help me?
What am I missing here?
Thanks


...what table name has been declared in your 'anuncio' model? ... also would be very helpful to paste the relation function here to see what you've declared there.

bettor
0

#3 User is offline   transistor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 78
  • Joined: 05-June 09
  • Location:Mexico

Posted 28 February 2010 - 02:41 PM

@bettor: sorry, my mistake. Instead of anuncio it should be table3
$condition = 'table1.someField = someValue';
$anuncios = new CActiveDataProvider('table3', array(
	'criteria'=>array(
		'condition'=>$condition,
		// I am using table 2 also, so:
		'with'=>'table2',
		'together'=>true,
	),
	'pagination'=>array(
		'pageSize'=>self::PAGE_SIZE,
	),
	'sort'=>$sort,
));

The relation defined in the models are:
Table3:
'table2' => array(self::BELONGS_TO, 'table2', 'id'),

Table2:
'table3' => array(self::HAS_MANY, 'table3', 'table_2'),
'table1' => array(self::BELONGS_TO, 'table1', 'id'),

Table1:
'table2' => array(self::HAS_MANY, 'table2', 'id'),

I want to have this condition:
table1.someField = someValue

How can I do this?
Thanks
0

#4 User is offline   transistor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 78
  • Joined: 05-June 09
  • Location:Mexico

Posted 03 March 2010 - 03:31 AM

Sorry for the *bump*
I haven't figured this one out.
0

#5 User is offline   transistor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 78
  • Joined: 05-June 09
  • Location:Mexico

Posted 09 March 2010 - 01:41 AM

*bump* again
someone? please?
0

#6 User is offline   jsoo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 01-March 10
  • Location:Durham, NC, USA

Posted 09 March 2010 - 06:44 AM

Guess from a Yii noob:

'with' => array(
    'table2',
    'table2.table1'
),


or maybe:

$condition = 'table2.table1.someField = someValue';

0

#7 User is offline   mikelimassol 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 08-January 10

Posted 09 March 2010 - 03:56 PM

i have a similar problem. with() for HAS_MANY relation does not seem to work with cactivedataprovider. i found a solution by reading this thread.
http://www.yiiframework.com/forum/index.php?/topic/7420-undestanding-relational-queries-with-cactivedataprovider/"
i hope it works for you too.

View Postjsoo, on 09 March 2010 - 06:44 AM, said:

Guess from a Yii noob:

'with' => array(
    'table2',
    'table2.table1'
),


or maybe:

$condition = 'table2.table1.someField = someValue';


0

#8 User is offline   mikelimassol 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 08-January 10

Posted 09 March 2010 - 04:01 PM

i had a similar problem. the with() association for HAS_MANY relation does not seem to work with cactivedataprovider. i found a solution by reading this thread.
http://www.yiiframework.com/forum/index.php?/topic/7420-undestanding-relational-queries-with-cactivedataprovider/"
i hope it works for you too.


View Postjsoo, on 09 March 2010 - 06:44 AM, said:

Guess from a Yii noob:

'with' => array(
    'table2',
    'table2.table1'
),


or maybe:

$condition = 'table2.table1.someField = someValue';


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