Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the Yii way to CModel lists (of other CModel lists or not)?
    text
    copied!<p>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.</p> <p>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)</p> <p><strong>2 reasons, lists are good:</strong></p> <ul> <li>I am looking for something like <strong>CModelList</strong> to extend when having objects with a fair amount of logic which cannot be tackled with <em>relations</em> (which is a CActiveRecord element anyway) </li> <li>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 <strong>ids and load only subset</strong></li> </ul> <p><strong>What does not seem to solve the problem</strong></p> <ul> <li><a href="http://www.yiiframework.com/doc/guide/1.1/en/database.arr" rel="nofollow">Relational Active Record</a>: because not all models are active records</li> <li><a href="http://www.yiiframework.com/doc/api/1.1/CAttributeCollection" rel="nofollow">CAttributeCollection</a>: because it only supports all objects in memory like arrays</li> <li>Same methods within different models such as getRestaurantsAverageRatingByPriceRange: because that is functional programming within OOP growing rapidly as Models have more methods</li> </ul> <p><strong>Example 1</strong></p> <pre><code>$user = User::model()-&gt;findByPk($userID); // get a user $restaurantList = $user-&gt;getRestaurants(); // get restaurants for that user for($i=0;$i&lt;5;$i++) { $this-&gt;renderPartial( "rating", array("rating" =&gt; $restaurantList-&gt;getAverageRatingByPriceRange( $i ) ); } </code></pre> <p><strong>Example 2</strong> (same list logic, different base model)</p> <pre><code>$city = City::model()-&gt;findByPk($cityID); // get a city $restaurantList = $city-&gt;getRestaurants(); // get restaurants for that city for($i=0;$i&lt;5;$i++) { $this-&gt;renderPartial( "rating", array("rating" =&gt; $restaurantList-&gt;getAverageRatingByPriceRange( $i ) ); } </code></pre> <p><strong>Example 3</strong> (same list type and base model, different logic ) </p> <pre><code>$user = User::model()-&gt;findByPk($userID); // get a user $restaurantList = $user-&gt;getRestaurants(); // get restaurants for that user $this-&gt;renderPartial( "map", array("coord" =&gt; $restaurantList-&gt;getCoordinatesMap() ); </code></pre> <p>So is it missing in Yii, should I start thinking differently or what is the mechanism to use and keep clean code and structure?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload