當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Category::findByName方法代碼示例

本文整理匯總了PHP中app\modules\shop\models\Category::findByName方法的典型用法代碼示例。如果您正苦於以下問題:PHP Category::findByName方法的具體用法?PHP Category::findByName怎麽用?PHP Category::findByName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在app\modules\shop\models\Category的用法示例。


在下文中一共展示了Category::findByName方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的app\modules\shop\models\Category::findByName方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。