3

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Nicolas Grasset
  • 453
  • 2
  • 11
  • 1
    You might want to define custom class(es) and group methods by logic and use Query Builder (or dbCriteria) to create your queries based on the data type so you can use the same methods for different types of data. I use relations primarily to fetch related data (not logic) and scopes for pre-defined queries/filtering (you can also combine them as needed), but you are always going to incur overhead with AR and very complex queries will require db->createCommand anyway. – ldg Aug 17 '11 at 00:29
  • Thanks Idg. This is what I have done so far, but I would like these classes to be Models. I am thinking about creating a CModelList class extending CModel. Just want to check I am not getting Yii wrong. Since my app is already 80% built, I am planning the refactoring. – Nicolas Grasset Aug 17 '11 at 08:39
  • I has a similar requirement and thought about the same but in the end just went with custom components and extended a couple of the models, as the extra overhead with CModel didn't seem worth it. I would certainly be interested seeing what you come up with. – ldg Aug 17 '11 at 19:26
  • 1
    Why not just add a behavior for that? any function that is not found in the actual class will be passed on to the behavior. I use this for my attachment logic for example. Allowing attachments to be attached to a whole variety of objects without having to write the functions every time. – Blizz May 19 '12 at 10:46

1 Answers1

2

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/

Pentium10
  • 204,586
  • 122
  • 423
  • 502