当前位置: 首页>>代码示例>>PHP>>正文


PHP Post::addTag方法代码示例

本文整理汇总了PHP中common\models\Post::addTag方法的典型用法代码示例。如果您正苦于以下问题:PHP Post::addTag方法的具体用法?PHP Post::addTag怎么用?PHP Post::addTag使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在common\models\Post的用法示例。


在下文中一共展示了Post::addTag方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: actionCreate

 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     $model->tags = [];
     // default values
     $model->allow_comment = 1;
     $model->is_public = 1;
     $model->content_category_id = Post::CATEGORY_NEWS;
     $model->user_id = Yii::$app->user->id;
     $model->is_pin = 0;
     $model->with_photo = 0;
     $model->with_video = 0;
     $matchModel = new \common\models\MatchSearch();
     $relation = new Relation();
     $relation->relationable_type = Relation::RELATIONABLE_POST;
     $matches = $matchModel::find()->orderBy(['date' => SORT_DESC])->limit(10)->all();
     $matchesList = [];
     foreach ($matches as $match) {
         $matchDate = date('d.m.Y', strtotime($match->date));
         $matchesList[$match->id] = $match->name . ' (' . $matchDate . ')';
     }
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         // Save the model to have a record number
         if (!$model->save()) {
             return $this->render('create', ['model' => $model]);
         }
         // Adding new tags
         if (is_array($model->tags)) {
             foreach ($model->tags as $id) {
                 $model->addTag($id);
             }
         }
         $cached_tag_list = [];
         $newTags = $model->getTags();
         foreach ($newTags as $newTag) {
             $cached_tag_list[] = $newTag->name;
         }
         $model->cached_tag_list = implode(', ', $cached_tag_list);
         // Set image
         $uploadedFile = UploadedFile::getInstance($model, 'image');
         if ($uploadedFile) {
             // Save origionals
             $asset = new Asset();
             $asset->assetable_type = Asset::ASSETABLE_POST;
             $asset->assetable_id = $model->id;
             $asset->uploadedFile = $uploadedFile;
             $asset->saveAsset();
             // Save thumbnails
             $imageID = $asset->id;
             $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_POST);
             foreach ($thumbnails as $thumbnail) {
                 $asset = new Asset();
                 $asset->parent_id = $imageID;
                 $asset->thumbnail = $thumbnail;
                 $asset->assetable_type = Asset::ASSETABLE_POST;
                 $asset->assetable_id = $model->id;
                 $asset->uploadedFile = $uploadedFile;
                 $asset->saveAsset();
             }
         }
         $relation->relationable_id = $model->id;
         if ($relation->load(Yii::$app->request->post()) && $model->validate()) {
             if ($relation->parent_id != '' && is_array($relation->parent_id)) {
                 $relation->parent_id = $relation->parent_id[0];
             }
             if ($relation->parent_id && is_numeric($relation->parent_id)) {
                 $relation->save();
             }
         }
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'relation' => $relation, 'matchModel' => $matchModel, 'matchesList' => $matchesList]);
     }
 }
开发者ID:alexsynytskiy,项目名称:Dynamomania,代码行数:80,代码来源:PostController.php

示例2: actionPostAdd

 /**
  * Url: /post/add
  * @return mixed
  * @throws ForbiddenHttpException
  */
 public function actionPostAdd()
 {
     if (Yii::$app->user->isGuest) {
         throw new ForbiddenHttpException("Вы не можете выполнить это действие.");
     }
     $model = new Post();
     $model->content_category_id = Post::CATEGORY_BLOG;
     $model->tags = [];
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $model->allow_comment = 1;
         $model->is_public = 1;
         $model->content_category_id = Post::CATEGORY_BLOG;
         $model->user_id = Yii::$app->user->id;
         // Set slug
         $model->slug = $model->genSlug($model->title);
         // Save the model to have a record number
         if ($model->save()) {
             // Adding new tags
             if (is_array($model->tags)) {
                 foreach ($model->tags as $id) {
                     $model->addTag($id);
                 }
             }
             $cached_tag_list = [];
             $newTags = $model->getTags();
             foreach ($newTags as $newTag) {
                 $cached_tag_list[] = $newTag->name;
             }
             $model->cached_tag_list = implode(', ', $cached_tag_list);
             // Set image
             $uploadedFile = UploadedFile::getInstance($model, 'image');
             if ($uploadedFile) {
                 // Save origionals
                 $asset = new Asset();
                 $asset->assetable_type = Asset::ASSETABLE_POST;
                 $asset->assetable_id = $model->id;
                 $asset->uploadedFile = $uploadedFile;
                 $asset->saveAsset();
                 // Save thumbnails
                 $imageID = $asset->id;
                 $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_POST);
                 foreach ($thumbnails as $thumbnail) {
                     $asset = new Asset();
                     $asset->parent_id = $imageID;
                     $asset->thumbnail = $thumbnail;
                     $asset->assetable_type = Asset::ASSETABLE_POST;
                     $asset->assetable_id = $model->id;
                     $asset->uploadedFile = $uploadedFile;
                     $asset->saveAsset();
                 }
             }
             $model->save(false);
             return $this->redirect($model->getUrl());
         }
         var_dump($model->getErrors());
         die;
     }
     $title = 'Добавить запись в блог';
     return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | ' . $title, 'columnFirst' => ['blog_form' => ['view' => '@frontend/views/forms/blog_form', 'data' => compact('model', 'tags', 'title')]], 'columnSecond' => ['blog' => SiteBlock::getBlogPosts(), 'banner1' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner2' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner3' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner4' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner5' => SiteBlock::getBanner(Banner::REGION_NEWS)]]);
 }
开发者ID:alexsynytskiy,项目名称:Dynamomania,代码行数:65,代码来源:SiteController.php


注:本文中的common\models\Post::addTag方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。