How To Add Where In Yii Model Search Method?

I have four tables in my DB as shown(user, user_test, test, status). There is a M:M to relation between user and test and therefore user_test is a Gerund between them.

The status table has statuses for both user_test and test tables. The tablename field in status table shows which table the status belongs to as shown in the lower image.

I want to show table user_test in a CGridView with the related data in all the three tables user, test and status. All is well as the relations are correct.

Problem: When I want to show in my CGrid like:





It gives me name ‘completed’ but it is the status of table ‘test’ and the correct one should be ‘confirmed’ as status_id in user_test is 2.

Any help?

!Database design

!Database design

public function search()


	// Warning: Please modify the following code to remove attributes that

	// should not be searched.

	$criteria=new CDbCriteria;








	$criteria->compare('user.signum',$this->signum, FALSE);

	$criteria->compare('',$this->email, FALSE);

	$criteria->compare('test.seats',$this->seats, FALSE);


	return new CActiveDataProvider($this, array(






					'desc'=>'user.signum DESC',




					'desc'=>' DESC',




					'desc'=>'test.seats DESC'







Sounds to me as if relations were not correct, although you state otherwise. Did you turn on logging and check the actual queries. Should be easy to track where this value is coming from.

btw. The proper type for status.tablename would probably be ENUM("user_test", "test"), instead of VARCHAR.

How to turn on logging and check queries? thanks









in your configuration. And then add a log route, for example CWebLogRoute (shows logs directly on website):

    'log' => array(

            'class' => 'CLogRouter',

            'routes' => array(






Querying SQL: SELECT AS t0_c0, t.user_id AS t0_c1,

t.test_id AS t0_c2, t.status_id AS t0_c3, t.bonus AS

t0_c4, t.hash AS t0_c5, t.timeslots_id AS t0_c6,

AS t1_c0, user.signum AS t1_c1, AS t1_c2,

user.pnr AS t1_c3, user.birth_year AS t1_c4, user.gender AS

t1_c5, user.ericsson_employee AS t1_c6, AS t1_c7,

user.work_profile AS t1_c8, user.registration_date AS t1_c9,

user.de_registration_date AS t1_c10, user.available AS t1_c11,

user.test_type_id AS t1_c12, user.password AS t1_c13,

user.roles AS t1_c14, user.username AS t1_c15, AS

t2_c0, AS t2_c1, test.seats AS t2_c2,

test.timeslots_id AS t2_c3, test.status_id AS t2_c4,

test.test_type_id AS t2_c5, test.publish_date AS t2_c6,

test.publish_time AS t2_c7, AS t3_c0,

AS t3_c1, status.value AS t3_c2, status.sequence AS t3_c3,

status.tablename AS t3_c4 FROM user_test t LEFT OUTER JOIN

user user ON ( LEFT OUTER JOIN test test

ON ( LEFT OUTER JOIN status status ON

( LIMIT 10


UPDATED: The sql I want to generate is:

             SELECT * FROM user_test AS UserTest

             INNER JOIN user ON

             INNER JOIN test on

             INNER JOIN (SELECT status.value, name from status where status.tablename='user_test') AS Status ON (Status.value = UserTest.status_id)

use addincondition,in search method for where clause.

I have given the SQL can you please write the addincondition for it?