Accessing attached bahavior property

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?

A couple of changes I tried seems to fix it.

  1. Keep the same behavior instance per user

  2. Accessor method (I don’t know why the array property cannot be assigned to directly)




public function actionResults() {

  /* Snipped generation of search criteria */


  $values = Field_Value::model()->with('owner', 'field')->findAll($dbCriteria);

  $results = array();

  foreach($values as $v) {

    if(!in_array($v->owner, $results))

      $v->owner->attachbehavior('match', 'application.components.SearchResult');

    $v->owner->add($v); // $v->owner->matching_field_values[] = $v;

    if(!in_array($v->owner, $results))

      $results[] = $v->owner; }

    $this->render('basicresults', array('results'=>$results));              

  }

}






class SearchResult extends CActiveRecordbehavior

{

  public $matching_field_values = array();

  public function add($v) {

    $this->matching_field_values[] = $v;

  ]

}



/Tommy

Thanks Tommy, that fixed it!

Do you think we should report the array appending issue as a bug? I don’t really see why it should be necessary to access it through a setter function, and not directly…

I noticed the property can be set this way




  $v->user->match->matching_field_values[] = $v;



Bug or feature? <_<

/Tommy

You can open a ticket http://code.google.com/p/yii/issues/list