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?