how to createSearchCondition for a relation column ?

Hi,

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%''; 


$addressList=Address::model()->with('company')->findAll($criteria);


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



$model=Address::model();


$schema=$model->getTableSchema();


$builder=$model->getCommandBuilder();





$relation=$model->getActiveRelation('company');


$relationclassname=$relation->className;


$tablename=CActiveRecord::model($relationclassname)->tableName();





$columns=array('0'=>'Company');


$keywords=array('0'=>'mysearchcompany');


$condition=$builder->createSearchCondition($tablename,$columns,$keywords,$prefix='company.');





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

I would create another model method to get specific results

Address::model()->findAllByCompanyLike($mycompany)

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.

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

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

Something like this



$columns=array('0'=>'company.Company');


$keywords=array('0'=>'mysearchcompany');


$condition=$builder->createSearchCondition($schema,$columns,$keywords,$prefix=null);


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

$schema=Address::model()->getDbConnection()->getSchema();

regards Horizons

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.

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.