how to createSearchCondition for a relation column ?

(Horizons Horizons) #1


I have one question (maybe its easy and I am just too blind).

I have a Model Address with a relation like below

 'company'=>array(self::BELONGS_TO, 'Company', 'CompanyID','alias'=>'company'),

And now I want to create a query to show specific Address entries with a specific company which should be searchable e.g. search for 'mysearchcompay'.

$criteria->condition='company.Company Like '%mysearchcompany%''; 


This works like expected and also give the correct results i wanted.

But I wanted to create the search condition dynamic way with 'createSearchCondition'.

But the only way to get the right select is like below sadly the commandbuilder only accecpts the tablename or the right CMysqlTableSchema










will give (company.`Company` LIKE '%mysearchcompany%')

Isn't there an easy way to get to this result?

It also would be helpfull if the tablename is given in the relation.

regards Horizons

(Marko Bischof) #2

I would create another model method to get specific results


(Horizons Horizons) #3

i can't create a model method, the whole creation of the condition and the select is done in a class which i use to fill a dynamic created table with changeing columns and searchoptions which are defined through the model used and some parameters.

(Qiang Xue) #4

Your code should be fine. It is lengthy because most of the code is to obtain the table name of the relation.

(Horizons Horizons) #5

I really thought that there is a “shorter” possibility :(

Something like this




But then it says that my address table hasn't the column company.Company instead using this as a searchoption for the relation with the company table wich has the alias "company".

Also it could be shorten if the tablename is also given directly in the relation object. I mean all the tables has been meta selected and are in the


regards Horizons

(Qiang Xue) #6

nope, your first $schema refers to the primary table, not the related one.

you second $schema is different. It refers to the whole database schema.

(Horizons Horizons) #7

I knew that the second schema hold "all" meta data of the tables which are used on the current page.

I thought that if i already know all the meta data of the second table (company in my case) and have its alias name and the model class set in the relation of the address model. Why there is no "easier" possibility to create a searchoption through the schema of the address model itself.

It should look if in the column name is a '.' point and look into their relation for the right alias and creates the correct condition for it.

Similar to CSort where i also have the option to sort for "company.Company" for this related column.

Otherwise i have to split the searchoptions and look for a '.' and make the code from the 1st post.

regards Horizons.