Question

I have been playing with Yii for a couple of months now, and to structure the code the Yii way, I was wondering how to best have models being lists of other models.

Yii seems to have models very close to DAO conceptually while the MVC design pushes not to put non-DAO models anywhere else than with models. (I am no MVC expert)

2 reasons, lists are good:

  • I am looking for something like CModelList to extend when having objects with a fair amount of logic which cannot be tackled with relations (which is a CActiveRecord element anyway)
  • Lists as a type can apply logic to elements while not having to load them all in memory at once and still offer a single type to play with across the code with ids and load only subset

What does not seem to solve the problem

  • Relational Active Record: because not all models are active records
  • CAttributeCollection: because it only supports all objects in memory like arrays
  • Same methods within different models such as getRestaurantsAverageRatingByPriceRange: because that is functional programming within OOP growing rapidly as Models have more methods

Example 1

$user = User::model()->findByPk($userID);  // get a user
$restaurantList = $user->getRestaurants(); // get restaurants for that user

for($i=0;$i<5;$i++) {
   $this->renderPartial( "rating", 
      array("rating" => $restaurantList->getAverageRatingByPriceRange( $i ) );
}  

Example 2 (same list logic, different base model)

$city = City::model()->findByPk($cityID);  // get a city
$restaurantList = $city->getRestaurants(); // get restaurants for that city

for($i=0;$i<5;$i++) {
   $this->renderPartial( "rating", 
      array("rating" => $restaurantList->getAverageRatingByPriceRange( $i ) );
}  

Example 3 (same list type and base model, different logic )

$user = User::model()->findByPk($userID);  // get a user
$restaurantList = $user->getRestaurants(); // get restaurants for that user


$this->renderPartial( "map", 
   array("coord" => $restaurantList->getCoordinatesMap() );

So is it missing in Yii, should I start thinking differently or what is the mechanism to use and keep clean code and structure?

Was it helpful?

Solution

Use of Component Behavior

A component supports the mixin pattern and can be attached with one or several behaviors. A behavior is an object whose methods can be 'inherited' by its attached component through the means of collecting functionality instead of specialization (i.e., normal class inheritance). A component can be attached with several behaviors and thus achieve 'multiple inheritance'.

Behavior classes must implement the IBehavior interface. Most behaviors can extend from the CBehavior base class. If a behavior needs to be attached to a model, it may also extend from CModelBehavior or CActiveRecordBehavior which implements additional features specifc for models.

To use a behavior, it must be attached to a component first by calling the behavior's attach() method. Then we can call a behavior method via the component:

// $name uniquely identifies the behavior in the component
$component->attachBehavior($name,$behavior);
// test() is a method of $behavior
$component->test();

An attached behavior can be accessed like a normal property of the component. For example, if a behavior named tree is attached to a component, we can obtain the reference to this behavior object using:

$behavior=$component->tree;
// equivalent to the following:
// $behavior=$component->asa('tree');

A behavior can be temporarily disabled so that its methods are not available via the component. For example,

$component->disableBehavior($name);
// the following statement will throw an exception
$component->test();
$component->enableBehavior($name);
// it works now
$component->test();

It is possible that two behaviors attached to the same component have methods of the same name. In this case, the method of the first attached behavior will take precedence.

When used together with events, behaviors are even more powerful. A behavior, when being attached to a component, can attach some of its methods to some events of the component. By doing so, the behavior gets a chance to observe or change the normal execution flow of the component.

A behavior's properties can also be accessed via the component it is attached to. The properties include both the public member variables and the properties defined via getters and/or setters of the behavior. For example, if a behavior has a property named xyz and the behavior is attached to a component $a. Then we can use the expression $a->xyz to access the behavior's property.

More reading:
http://www.yiiframework.com/wiki/44/behaviors-events
http://www.ramirezcobos.com/2010/11/19/how-to-create-a-yii-behavior/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top