Esempio n. 1
0
 public function saveContent($model, $bodyModel)
 {
     $postDatas = Yii::$app->request->post();
     
     if ($model->load($postDatas) && $bodyModel->load($postDatas) && $model->validate() && $bodyModel->validate())
     {
         if ($model->summary === null || $model->summary === '')
         {
             if ($bodyModel->hasAttribute('body'))
             {
                 $content = strip_tags($bodyModel->body);
                 $pattern = '/\s/'; // 去除空白
                 $content = preg_replace($pattern, '', $content);
                 $model->summary = StringHelper::subStr($content, 250);
             }
         }
         if ($model->save())
         {
             $bodyModel->content_id = $model->id;
             $bodyModel->save();
             
             return $this->redirect([
                 'index'
             ]);
         }
     }
     return false;
 }
Esempio n. 2
0
 public function beforeSave($insert)
 {
     $uploadedFile = Common::uploadFile('Content[thumb]');
     if ($uploadedFile != null) {
         $this->thumb = $uploadedFile['url'] . $uploadedFile['new_name'];
     }
     if ($this->views == null) {
         $this->views = 0;
     }
     if ($this->comments == null) {
         $this->comments = 0;
     }
     if ($this->summary == null || empty($this->summary)) {
         $content = strip_tags($this->content);
         $pattern = '/\\s/';
         //去除空白
         $content = preg_replace($pattern, '', $content);
         $this->summary = StringHelper::subStr($content, 250);
     }
     return parent::beforeSave($insert);
 }