diff --git a/backend/models/User.php b/backend/models/User.php index 7c9edff6..4fc4e37a 100644 --- a/backend/models/User.php +++ b/backend/models/User.php @@ -13,6 +13,7 @@ use common\helpers\Util; use yii\base\Event; use yii\web\ForbiddenHttpException; +use yii\web\UploadedFile; /** * User model @@ -23,6 +24,7 @@ * @property string $password_reset_token * @property string $email * @property string $auth_key + * @property string $avatar * @property integer $status * @property integer $created_at * @property integer $updated_at @@ -96,6 +98,14 @@ public function attributeLabels() ]; } + public function beforeValidate() + { + if($this->avatar !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片 + $this->avatar = UploadedFile::getInstance($this, "avatar"); + } + return parent::beforeValidate(); + } + public function beforeSave($insert) { Util::handleModelSingleFileUpload($this, 'avatar', $insert, '@admin/uploads/avatar/'); diff --git a/common/models/Article.php b/common/models/Article.php index bbf3bd4f..45852d6e 100644 --- a/common/models/Article.php +++ b/common/models/Article.php @@ -15,6 +15,7 @@ use Yii; use common\libs\Constants; use yii\behaviors\TimestampBehavior; +use yii\web\UploadedFile; /** * This is the model class for table "{{%article}}". @@ -109,7 +110,6 @@ public function rules() 'title', 'sub_title', 'summary', - 'thumb', 'seo_title', 'seo_keywords', 'seo_description', @@ -295,6 +295,14 @@ public function afterFind() parent::afterFind(); } + public function beforeValidate() + { + if ($this->thumb !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片 + $this->thumb = UploadedFile::getInstance($this, "thumb"); + } + return parent::beforeValidate(); + } + public function beforeSave($insert) { if ($this->thumb) {