Example #1
0
 /**
  * 将文章插入数据库
  * @param $title
  * @param $content
  * @param $publish_at
  * @param $tag
  * @return bool
  */
 public static function insert($title, $content, $publish_at, $tag = '')
 {
     //插入标签(搜索的分类)
     $article = new Article();
     $article->title = $title;
     $article->content = $content;
     $article->author = 'yang';
     $article->status = Article::STATUS_GATHER;
     $article->publish_at = $publish_at;
     $res = $article->save(false);
     if ($tag) {
         try {
             $tagModel = Tag::find()->where(['name' => $tag])->one();
             if (!$tagModel) {
                 $tagModel = new Tag();
                 $tagModel->name = $tag;
                 $tagModel->article_count = 0;
                 $tagModel->save(false);
             }
             $articleTag = new ArticleTag();
             $articleTag->article_id = $article->id;
             $articleTag->tag_id = $tagModel->id;
             $articleTag->save(false);
         } catch (\Exception $e) {
             echo $e->getMessage() . PHP_EOL;
         }
     }
     return $res ? true : false;
 }
 /**
  * Creates a new Article model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Article();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model, 'categories' => ArticleCategory::find()->active()->all(), 'ads' => Ad::find()->all(), 'articles' => Article::find()->all()]);
     }
 }
Example #3
0
 /**
  * Creates a new ArticleDownload model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($cat_id)
 {
     $model = new ArticleDownload();
     $Article = new Article();
     $postData = Yii::$app->request->post();
     if ($model->load($postData) && $Article->load($postData) && $Article->save() && $model->addData($Article->article_id)) {
         Yii::$app->session->setFlash('success', '添加成功');
         return $this->redirect(['article/index']);
     } else {
         Yii::$app->view->params['meta_title'] = '添加文章';
         return $this->render('create', ['model' => $model, 'Article' => $Article]);
     }
 }
 public function actionCreate()
 {
     //储存视频
     if ($video = Yii::$app->request->post()) {
         $model = new Article();
         $files = Fileupload::upload();
         $video['img'] = isset($files[0]) ? $files[0] : './img/default.jpg';
         $model->set_video_data($video);
         if ($model->save(false)) {
             return $this->render('create', ['msg' => '添加视频成功!']);
         } else {
             return $this->render('create', ['msg' => '添加视频失败,请刷新重试!']);
         }
     } else {
         return $this->render('create');
     }
 }
Example #5
0
 /**
  * Creates a new Article model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Article();
     $dataModel = new ArticleData();
     if ($model->load(Yii::$app->request->post()) && $dataModel->load(Yii::$app->request->post())) {
         $isValid = $model->validate();
         if ($isValid) {
             $model->save(false);
             $dataModel->id = $model->id;
             $isValid = $dataModel->validate();
             if ($isValid) {
                 $dataModel->save(false);
                 return $this->redirect(['index']);
             }
         }
     }
     return $this->render('create', ['model' => $model, 'dataModel' => $dataModel]);
 }
Example #6
0
 /**
  * 将文章插入数据库
  * @param $title 标题
  * @param $content 内容
  * @param $publish_at 发布时间
  * @param string $category 分类名
  * @param string $cover 封面
  * @return int
  */
 public function insert($title, $content, $publish_at, $category = '', $cover = '')
 {
     //插入标签(搜索的分类)
     $categoryId = (new Category())->getCategoryIdByName($category);
     if (!$categoryId) {
         throw new Exception('该分类不存在');
     }
     $article = new Article();
     $article->title = $title;
     $article->author = '匿名';
     $article->status = 1;
     $article->category = $category;
     $article->category_id = $categoryId;
     $article->source = $this->config['domain'];
     $article->cover = $cover;
     $article->created_at = $publish_at;
     $article->user_id = 0;
     $res = $article->save(false);
     if ($res) {
         $articleData = new ArticleData();
         $articleData->id = $article->id;
         $articleData->content = $content;
         $res = $articleData->save(false);
     }
     return $res ? 1 : 0;
 }
 public function actionAddNewsFromParser()
 {
     $result = 0;
     $file = Yii::getAlias('@json') . DIRECTORY_SEPARATOR . 'import.json';
     if (file_exists($file)) {
         $ImportModel = file_get_contents($file);
         $obj = json_decode($ImportModel);
         //vd($obj);
         if ($ImportModel) {
             foreach ($obj as $row) {
                 $duble = Blog::getDublicateByTitle($row->title);
                 if (!$duble) {
                     $model = new Article();
                     $model->title = $row->title;
                     $model->image = $row->image ? $row->image : '';
                     $model->content = $row->content;
                     $model->created_at = $row->created_at;
                     $model->updated_at = $row->updated_at;
                     $model->author = $row->author;
                     $model->save();
                     $result = 1;
                 } else {
                     //echo "It is Dublicate", PHP_EOL;
                 }
             }
         }
     }
     return $result;
 }