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


PHP Category::findBySlug方法代码示例

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


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

示例1: unpackCategories

 /**
  * Makes an array of category ids from string
  *
  * @param array $fields
  * @param $multipleValuesDelimiter
  * @param array $additionalFields
  * @return array|bool
  */
 private function unpackCategories(array $fields, $multipleValuesDelimiter, array $additionalFields)
 {
     $categories = isset($fields['categories']) ? $fields['categories'] : (isset($fields['category']) ? $fields['category'] : false);
     if ($categories === false) {
         return $this->getCategoryIds();
     }
     if ($categories !== false) {
         if (strpos($categories, $multipleValuesDelimiter) > 0) {
             $categories = explode($multipleValuesDelimiter, $categories);
         } elseif (strpos($multipleValuesDelimiter, '/') === 0) {
             $categories = preg_split($multipleValuesDelimiter, $categories);
         } else {
             $categories = [$categories];
         }
         $typecast = 'id';
         if (isset($additionalFields['categories'])) {
             if (isset($additionalFields['categories']['processValuesAs'])) {
                 $typecast = $additionalFields['categories']['processValuesAs'];
             }
         }
         if ($typecast === 'id') {
             $categories = array_map('intval', $categories);
         } elseif ($typecast === 'slug') {
             $categories = array_map('trim', $categories);
             $categoryIds = [];
             foreach ($categories as $part) {
                 $cat = Category::findBySlug($part, 1, -1);
                 if (is_object($cat)) {
                     $categoryIds[] = $cat->id;
                 }
                 unset($cat);
             }
             $categories = array_map('intval', $categoryIds);
         } elseif ($typecast === 'name') {
             $categories = array_map('trim', $categories);
             $categoryIds = [];
             foreach ($categories as $part) {
                 $cat = Category::findByName($part, 1, -1);
                 if (is_object($cat)) {
                     $categoryIds[] = $cat->id;
                 }
                 unset($cat);
             }
             $categories = array_map('intval', $categoryIds);
         } else {
             // that's unusual behavior
             $categories = false;
         }
         // post-process categories
         // find & add parent category
         // if we need to show products of child categories in products list
         /** @var ShopModule $module */
         $module = Yii::$app->getModule('shop');
         if (is_array($categories) && $module->showProductsOfChildCategories) {
             do {
                 $repeat = false;
                 foreach ($categories as $cat) {
                     $model = Category::findById($cat, null, null);
                     if ($model === null) {
                         echo "\n\nUnknown category with id " . intval($cat) . " for model with id:" . $this->id . "\n\n";
                         continue;
                     }
                     if (intval($model->parent_id) > 0 && in_array($model->parent_id, $categories) === false) {
                         $categories[] = $model->parent_id;
                         $repeat = true;
                     }
                     unset($model);
                 }
             } while ($repeat === true);
         }
     }
     return $categories;
 }
开发者ID:Razzwan,项目名称:dotplant2,代码行数:81,代码来源:Product.php

示例2: getNextPart

 /**
  * @inheritdoc
  */
 public function getNextPart($full_url, $next_part, &$previous_parts)
 {
     $next_parts = explode("/", $next_part);
     $gathered_parts = [];
     $previous_model = null;
     $cacheTags = [];
     $root_id = 0;
     $title_append = "";
     /** @var Category $root_category */
     $root_category = null;
     if ($this->include_root_category === false) {
         $root_category = Category::findRootForCategoryGroup($this->category_group_id);
         if (is_object($root_category)) {
             $root_id = $root_category->id;
             $cacheTags[] = ActiveRecordHelper::getObjectTag(Category::className(), $root_id);
         } else {
             return false;
         }
     }
     foreach ($next_parts as $slug) {
         if (empty($slug)) {
             break;
         }
         $model = null;
         if ($previous_model === null) {
             $model = Category::findBySlug($slug, $this->category_group_id, $root_id);
         } else {
             $model = Category::findBySlug($slug, $this->category_group_id, $previous_model->id);
         }
         if ($model === null) {
             // выходим из цикла - тут никого нет
             break;
         }
         $cacheTags[] = ActiveRecordHelper::getObjectTag(Category::className(), $model->id);
         if (!empty($model->title_append)) {
             $title_append = $model->title_append;
         }
         $gathered_parts[$slug] = $model->id;
         $previous_model = $model;
     }
     if (count($gathered_parts) === 0) {
         if ($this->include_root_category === false) {
             $this->parameters['last_category_id'] = $root_category->id;
             $part = new $this(['gathered_part' => '', 'rest_part' => $next_part, 'parameters' => $this->parameters, 'cacheTags' => $cacheTags]);
             return $part;
         }
         return false;
     }
     $this->parameters['categories'] = [];
     if (!empty($title_append)) {
         $this->parameters['title_append'] = [$title_append];
     }
     $last_category_id = null;
     foreach ($gathered_parts as $slug => $id) {
         $this->parameters['categories'][] = $id;
         $last_category_id = $id;
     }
     $this->parameters['last_category_id'] = $last_category_id;
     $full_categories_url = implode("/", array_keys($gathered_parts));
     $part = new $this(['gathered_part' => $full_categories_url, 'rest_part' => mb_substr($next_part, mb_strlen($full_categories_url)), 'parameters' => $this->parameters, 'cacheTags' => $cacheTags]);
     return $part;
 }
开发者ID:tqsq2005,项目名称:dotplant2,代码行数:65,代码来源:CategoryPart.php


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