yiisoft/yii — Issue #393: Blog tutorial code example unclear due to change in convention
Author: caedmon5
Created: 2012-02-24 06:56:22+00:00
State: open

In the page Customizing Post Model, section "representing status in text" there is a section of code that is unclear because it violates the conventions used elsewhere in the tutorial. This issue is in the code chunk for the Lookup model. The text says "We modify the Lookup model class as follows to more easily access the textual data in the table" but there is no elipsis in the code to indicate that the sample code is to be added into the existing Lookup model rather than replace it.

In previous examples, the convention has been to include an elipsis in the code sample indicating the code that is to remain and to include entire methods, classes, etc. without elipsis when the code is to replace the existing code.

Here is the existing code (replacement follows):

[code]
class Lookup extends CActiveRecord
{
private static $_items=array();

public static function items($type)
{
if(!isset(self::$_items[$type]))
self::loadItems($type);
return self::$_items[$type];
}

public static function item($type,$code)
{
if(!isset(self::$_items[$type]))
self::loadItems($type);
return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false;
}

private static function loadItems($type)
{
self::$_items[$type]=array();
$models=self::model()->findAll(array(
'condition'=>'type=:type',
'params'=>array(':type'=>$type),
'order'=>'position',
));
foreach($models as $model)
self::$_items[$type][$model->code]=$model->name;
}
}
[/code]

The code should read

[code]
class Lookup extends CActiveRecord
{

.....

private static $_items=array();

public static function items($type)
{
if(!isset(self::$_items[$type]))
self::loadItems($type);
return self::$_items[$type];
}

public static function item($type,$code)
{
if(!isset(self::$_items[$type]))
self::loadItems($type);
return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false;
}

private static function loadItems($type)
{
self::$_items[$type]=array();
$models=self::model()->findAll(array(
'condition'=>'type=:type',
'params'=>array(':type'=>$type),
'order'=>'position',
));
foreach($models as $model)
self::$_items[$type][$model->code]=$model->name;
}
}
[/code]