如何在Yii中为相同的AR模型建立规则

艾奥努特·弗拉维乌斯·波加西亚人

我用Yii MVC

我必须经常在数据表中添加新列。

我想使用Gii并重新生成,model并保留自己制作的文字;诸如常量,变量,规则,关系等的变化...

因此,让我们以url类模型为例:

<?php

/**
 * This is the model class for table "url".
 *
 * The followings are the` available columns in table 'url':
 * @property string $id
 * @property integer $instance_id
 * @property integer $website_id
 * @property string $link
 * @property string $title
 * @property integer $created
 * @property integer $updated
 * @property integer $status
 */
class Url extends CActiveRecord {

    const ACTIVE = 1;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Url the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'url';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('instance_id, website_id, link, title, created, updated, status', 'required'),
            array('instance_id, website_id, created, updated, status', 'numerical', 'integerOnly' => true),
            array('link, title', 'length', 'max' => 255),
            array('link', 'unique'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, instance_id, website_id, link, title, created, updated, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'instance_id' => 'Instance',
            'website_id' => 'Website',
            'link' => 'Link',
            'title' => 'Title',
            'created' => 'Created',
            'updated' => 'Updated',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('instance_id', $this->instance_id);
        $criteria->compare('website_id', $this->website_id);
        $criteria->compare('link', $this->link, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('created', $this->created);
        $criteria->compare('updated', $this->updated);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

}

当我重新生成时model,theconstrulewithunique将会丢失

因此,我创建了一个新的model classUrlM扩展了urlModel类,该类保留了我所有的增强功能:

<?php

class UrlM extends Url {

    const ACTIVE = 1;

    public function rules() {
        return array(
            array('link', 'unique'),
        );
    }

    public function relations() {
        return array(
        );
    }

}

我如何Concat的从规则url模型类与rulesurlm模型类?

将来的增强功能应该使用相同的答案,例如 relations

塞尔吉·扎哈奇琴科(Serge Zaharchenko)

父类的每个函数(例如rules()relations()等等)都将返回数组。因此,您可以使用对的调用parent::rules(),然后仅加入“数组”。

public function rules() {
  $myParentRules=parent::rules();
  $myConcatRules=$myParentRules + array(
    array('link', 'unique'),
  );
  return $myConcatRules;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在extjs中为json数组建立模型?

来自分类Dev

Yii中的条件模型规则

来自分类Dev

如何在YII2中建立动态菜单

来自分类Dev

如何在Laravel中建立3模型之间的关系?

来自分类Dev

如何在ElasticSearch中建立社交关系模型?

来自分类Dev

如何在laravel中为相同的两个模型创建多个数据透视表?

来自分类Dev

Yii2 Active记录如何在mongo模型之间建立关系

来自分类Dev

如何在Yii中的urlManager规则内处理GET参数?

来自分类Dev

如何在yii中制定字母数字规则

来自分类Dev

如何在模型属性中查询相同模型的对象

来自分类Dev

如何在numpy中为其索引建立数组索引?

来自分类Dev

如何在Yii中的模型中从UserIdentity组件调用setState

来自分类Dev

如何在Realm中建立3个模型之间的关系模型?

来自分类Dev

Yii动态模型规则

来自分类Dev

如何在Rails表单中为两个模型的两个输入设置相同的值

来自分类Dev

如何在视图Yii中调用模型函数?

来自分类Dev

如何在视图yii框架中显示模型数组

来自分类Dev

如何在视图Yii中调用模型函数?

来自分类Dev

如何在yii2中访问模块的模型

来自分类Dev

如何在Yii中定义模型参数值?

来自分类Dev

如何在 yii 2 中更新相关模型

来自分类Dev

如何在makefile中为目标编写特定规则?

来自分类Dev

返回yii 2.0中指定的AR类的静态模型

来自分类Dev

如何在yii中与同一张表建立自我关系

来自分类Dev

Laravel 5.1,如何在模型中建立动态数据库连接

来自分类Dev

如何在Rails中的同一模型(用户)之间建立has_many:through关系?

来自分类Dev

如何在Ruby on Rails中的模型之间建立一对多关系?

来自分类Dev

如何在Python中建立热方程的3D模型?

来自分类Dev

如何在没有共同子元素的情况下在YANG中建立列表模型?

Related 相关文章

  1. 1

    如何在extjs中为json数组建立模型?

  2. 2

    Yii中的条件模型规则

  3. 3

    如何在YII2中建立动态菜单

  4. 4

    如何在Laravel中建立3模型之间的关系?

  5. 5

    如何在ElasticSearch中建立社交关系模型?

  6. 6

    如何在laravel中为相同的两个模型创建多个数据透视表?

  7. 7

    Yii2 Active记录如何在mongo模型之间建立关系

  8. 8

    如何在Yii中的urlManager规则内处理GET参数?

  9. 9

    如何在yii中制定字母数字规则

  10. 10

    如何在模型属性中查询相同模型的对象

  11. 11

    如何在numpy中为其索引建立数组索引?

  12. 12

    如何在Yii中的模型中从UserIdentity组件调用setState

  13. 13

    如何在Realm中建立3个模型之间的关系模型?

  14. 14

    Yii动态模型规则

  15. 15

    如何在Rails表单中为两个模型的两个输入设置相同的值

  16. 16

    如何在视图Yii中调用模型函数?

  17. 17

    如何在视图yii框架中显示模型数组

  18. 18

    如何在视图Yii中调用模型函数?

  19. 19

    如何在yii2中访问模块的模型

  20. 20

    如何在Yii中定义模型参数值?

  21. 21

    如何在 yii 2 中更新相关模型

  22. 22

    如何在makefile中为目标编写特定规则?

  23. 23

    返回yii 2.0中指定的AR类的静态模型

  24. 24

    如何在yii中与同一张表建立自我关系

  25. 25

    Laravel 5.1,如何在模型中建立动态数据库连接

  26. 26

    如何在Rails中的同一模型(用户)之间建立has_many:through关系?

  27. 27

    如何在Ruby on Rails中的模型之间建立一对多关系?

  28. 28

    如何在Python中建立热方程的3D模型?

  29. 29

    如何在没有共同子元素的情况下在YANG中建立列表模型?

热门标签

归档