Yii Framework Forum: Accessing attached bahavior property - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Accessing attached bahavior property Rate Topic: -----

#1 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 10 November 2009 - 05:42 AM

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

#2 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 10 November 2009 - 11:57 PM

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 11 November 2009 - 03:43 AM

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

#4 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 11 November 2009 - 09:20 AM

View PostSander, on 11 November 2009 - 03:43 AM, said:

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 10-October 08
  • Location:European Union

Posted 17 November 2009 - 07:29 AM

You can open a ticket http://code.google.c...yii/issues/list
Yii user #37
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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