返回yii 2.0中指定AR类的静态模型 [英] Returns the static model of the specified AR class in yii 2.0

查看:19
本文介绍了返回yii 2.0中指定AR类的静态模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 YII 2 构建博客应用程序,我使用 tbl_lookup 表来存储其他数据对象所需的整数值和文本表示之间的映射.我修改 Lookup 模型类如下,以便更轻松地访问表中的文本数据.这是我的代码:

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);
}

有人帮我吗?我的错在哪里.或者这里有人有一些使用 yii 2 制作博客应用程序的教程吗?谢谢.

does anyone to help me ? where's my mistake. or anyone here have some tutorial to make blog application using yii 2 ? thank you.

推荐答案

在 Yii2 中你不使用 model() 方法.因为,Yii2 和 Yii1 是不同的(更多见 https://github.com/yiisoft/yii2/blob/master/docs/guide/intro-upgrade-from-v1.md).在 Yii1 中提供这样的数据:

In Yii2 you don't use model() method. Because, Yii2 and Yii1 is different(see more https://github.com/yiisoft/yii2/blob/master/docs/guide/intro-upgrade-from-v1.md). In Yii1 to provide data like that:

Post::model()->find($condition,$params);

在 Yii2 中提供这样的数据:

In Yii2 to provide data like that:

Post::find()->where(['type' => $id])->all(); 

查看更多https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md

删除模型中的model:

public static function model($className=__CLASS__)
{
    return parent::model($className);
}

并将 loadItems 方法更改为:

private static function loadItems($type)
{
    self::$_items[$type]=[];
    $models=self::findAll([
        'condition'=>'type=:$type',
        'params'=>[':type'=>$type],
        'order'=>'position',
    ]);

    foreach ($models as $model)
        self::$_items[$type][$model->code]=$model->name;
}

这篇关于返回yii 2.0中指定AR类的静态模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆