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