yii2自动更新时间,根据条件设定指定值,接受多选框的值

gii自动生成的_form.php文件中,咱们能够根据代码$model->isNewRecord 返回的值,来判断当前是增长仍是更新,在form.php文件中,还能够根据它的属性值给字段input框赋予默认值php

connect字段为多选框字段,前台传到后台的数据默认是数组格式。该字段对应是让tostring方法处理,先把它的值赋给静态变量$connect,而后在beforeSave中把数组格式化成字符串,在返回,存入数据库。数据库

<?php数组

namespace backend\models;yii

use Yii;this

use \yii\db\ActiveRecord;spa

class Newdocument extends ActiveRecord
{orm

    public function beforeSave($insert){
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){//判断是更新仍是插入
                $this->connect = implode(',', $this->connect);
                $this->create_time = time();字符串

            }else{
                $this->update_time = time();
                $this->connect = implode(',', $this->connect);
            }
           return true;
        }else{input

            return false;
        }
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'document';
    }string

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[  'create_time','update_time'], 'integer'],

            ['connect','tostring'],
        ];
    }

    public function tostring(){//可经过方法单独控制某个字段,也能够直接经过beforesave方法控制             //if($this->isNewRecord){//判断是更新仍是插入                 //$this->connect = implode(',', $this->connect);             //}else{                // $this->connect = implode(',', $this->connect);             //}     }

相关文章
相关标签/搜索