Yii Framework Forum: how to createSearchCondition for a relation column ? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

how to createSearchCondition for a relation column ? Rate Topic: -----

#1 User is offline   horizons 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 122
  • Joined: 18-December 08

Posted 17 June 2009 - 03:55 AM

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


0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 08-May 09

Posted 17 June 2009 - 04:59 AM

I would create another model method to get specific results
Address::model()->findAllByCompanyLike($mycompany)
0

#3 User is offline   horizons 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 122
  • Joined: 18-December 08

Posted 17 June 2009 - 05:18 AM

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.


0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 17 June 2009 - 07:59 PM

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

0

#5 User is offline   horizons 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 122
  • Joined: 18-December 08

Posted 18 June 2009 - 02:04 AM

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
0

#6 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 18 June 2009 - 06:49 AM

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

#7 User is offline   horizons 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 122
  • Joined: 18-December 08

Posted 18 June 2009 - 07:17 AM

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.

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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