In my application I need to provide a search page which can search through different kinds of models (users, projects, groups, etc). I figured I’d attach a custom SearchResult behavior to each matching record, so I can present them next to each other through a generic API. This is my current setup (simplified):
components/SearchResult.php:
class SearchResult extends CActiveRecordBehavior {
public $matching_field_values = array();
}
controllers/SearchController.php:
public function actionResults() {
/* Snipped generation of search criteria */
$values = Field_Value::model()->with('owner', 'field')->findAll($dbCriteria);
$results = array();
foreach($values as $v) {
$v->owner->attachBehavior('match', 'application.components.SearchResult');
$v->owner->matching_field_values[] = $v;
if(!in_array($v->owner, $results)) { $results[] = $v->owner; }
}
$this->render('basicresults', array('results'=>$results));
}
Field_Value->owner is specified by a BELONGS_TO relation to a User model.
views/search/basicresults.php:
<?php foreach($results as $r): ?>
<h3><?php echo CHtml::encode($r->name); ?></h3>
<?php foreach($r->matching_field_values as $v): ?>
<u><?php echo CHtml::encode($v->field->label); ?></u>:
<?php echo CHtml::encode($v->value); ?><br />
<?php endforeach; ?>
<?php endforeach; ?>
Currently, the Users name is printed, but none of the Field_Values that were matched are displayed.
The strange thing is: When I print_r($r->matching_field_values), I get an empty array(), indicating the behavior is at least attached (otherwise it would be NULL, right?). However, When I test for $r->canGetProperty(‘matching_field_values’), it returns false.
Who can help me get this working?