i want to build blog application using YII 2, i use the tbl_lookup table to store the mapping between integer values and textual representations that are needed by other data objects. i modify the Lookup model class as follows to more easily access the textual data in the table. here my code :
<?php
namespace common\models;
use Yii;
?>
class Lookup extends \yii\db\ActiveRecord
{
private static $_items=array();
public static function tableName()
{
return '{{%lookup}}';
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'code', 'type', 'position'], 'required'],
[['code', 'position'], 'integer'],
[['name', 'type'], 'string', 'max' => 128]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'code' => 'Code',
'type' => 'Type',
'position' => 'Position',
];
}
private static function loadItems($type)
{
self::$_items[$type]=[];
$models=self::model()->findAll([
'condition'=>'type=:$type',
'params'=>[':type'=>$type],
'order'=>'position',
]);
foreach ($models as $model)
self::$_items[$type][$model->code]=$model->name;
}
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;
}
}
but i get some error when i want to return the static model class
public static function model($className=__CLASS__)
{
return parent::model($className);
}
does anyone to help me ? where's my mistake. or anyone here have some tutorial to make blog application using yii 2 ? thank you.