Yes i want to show, but that goal is achieved! What i don’t know how to do is have that column ( usedInRelation ) serchable and filterable. The commands in the top of the column are there but, not being the usedInRelation attribute a field in the db, the search gives an '‘unknown colum’ error if I put this row in the model’s search
$criteria->compare(‘usedInRelation’,$this->usedInRelation,true); This will not work if U dont have ‘usedInRelation’ in DB table, I think. Becasue "CDbCriteria represents a query criteria, such as conditions, ordering by, limit/offset. "
‘userInRelationSearch’ must be public and made ‘safe on search’ in model class rules. That’s why it’s always null, because U are doing mass assign and if attribute is not safe it will not be assigned to model.
this ‘someRelation’ should be real name of relation in your model class, please check this.
Actually, Yii , because I think that the way you are going is actually more complex and the extension is a real timesaver.
Still, it is not clear what you want to achieve.
If you want only records that are involved in a relation, then you can try ‘together’, ‘with’, ‘joinType’=>‘INNER JOIN’.
If you want to test records in the relation against a search value then you "need" RelatedSearchBehavior.
Below you can see the could that would need to be added to a standard Model:
class MyModel extends CActiveRecord {
public function rules()
{
return array
/* ...*/
array('relatedName,usedInRelation', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'track'=>array(self::HAS_ONE,'Track',array('TrackId'=>'TrackId')),
);
}
public $usedInRelation;
public function init() {
$this->usedInRelation = $this->relationName ? 1 : 0;
}
public function behaviors() {
return array(
// Add RelatedSearchBehavior
'relatedsearch'=>array(
'class'=>'RelatedSearchBehavior',
'relations'=>array(
'relatedName'=>'track.Name',
'relatedSearch'=>array(
'field'=>'track.used',
'searchvalue'=>'usedInRelation',
),
),
);
}
public function search()
{
/** Other compares ... */
/** Virtual attributes in the relations are searched automatically, no need to add them here. */
return $this->relatedSearch($criteria);
}
/**
* Not required, but to add autoscopes for attributes (uses RelatedSearchBehavior).
*/
public function __call($name,$parameters) {
try {
return parent::__call($name,$parameters);
} catch (CException $e) {
if(preg_match('/'.Yii::t('yii',quotemeta(Yii::t('yii','{class} and its behaviors do not have a method or closure named "{name}".')),array('{class}'=>'.*','{name}'=>'.*')).'/',$e->getMessage())) {
return $this->autoScope($name, $parameters);
} else {
throw $e;
}
}
}
}
The problem i didn’t get a value in trace is because the grid is update by ajax, so no new output but the value of $this->usedInRelationSearch is correct… i can see from the error I still get in the query;
the last problem is now that no value is passed for $this->id in
I am not sure that Yii uses the limit on relations, but you could add a "LIMIT 1" in your relation for this search to avoid bringing back all the records.