本文整理汇总了PHP中app\modules\shop\models\Category::findById方法的典型用法代码示例。如果您正苦于以下问题:PHP Category::findById方法的具体用法?PHP Category::findById怎么用?PHP Category::findById使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\modules\shop\models\Category
的用法示例。
在下文中一共展示了Category::findById方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
public function run()
{
$id = $this->getId();
$this->plugins = ArrayHelper::merge($this->plugins, ['checkbox']);
$items = [];
if (empty($this->selectedItems)) {
$parent = Category::findById(Yii::$app->request->get('parent_id'));
while ($parent instanceof Category) {
$this->selectedItems[] = $parent->id;
$parent = $parent->parent;
}
}
$this->routes['getTree'] = ArrayHelper::merge($this->routes['getTree'], ['selectedItems' => implode(',', $this->selectedItems)]);
if (isset($this->routes['edit'])) {
$items['edit'] = ['label' => Yii::t('app', 'Edit'), 'icon' => 'fa fa-pencil', 'action' => new JsExpression("function (a) {\n var \$object = \$(a.reference[0]);\n document.location = " . Json::encode(Url::to($this->routes['edit'])) . "\n + '?id=' + \$object.attr('data-id') + '&parent_id=' +\n \$object.attr('data-parent-id');\n }")];
}
if (isset($this->routes['open'])) {
$items['open'] = ['label' => Yii::t('app', 'Open'), 'icon' => 'fa fa-folder-open', 'action' => new JsExpression("function (a) {\n var \$object = \$(a.reference[0]);\n document.location = " . Json::encode(Url::to($this->routes['open'])) . "\n + '?parent_id=' + \$object.attr('data-id');\n }")];
}
if (isset($this->routes['create'])) {
$items['create'] = ['label' => Yii::t('app', 'Create'), 'icon' => 'fa fa-plus-circle', 'action' => new JsExpression("function (a) {\n var \$object = \$(a.reference[0]);\n document.location = " . Json::encode(Url::to($this->routes['create'])) . "\n + '?parent_id=' + \$object.attr('data-id');\n }")];
}
if (isset($this->routes['delete'])) {
$items['delete'] = ['label' => Yii::t('app', 'Delete'), 'icon' => 'fa fa-trash-o', 'action' => new JsExpression("function (a) {\n var \$object = \$(a.reference[0]);\n document.location = " . Json::encode(Url::to($this->routes['delete'])) . "\n + '?id=' + \$object.attr('data-id');\n }")];
}
$options = ['state' => ['key' => $this->stateKey], 'plugins' => $this->plugins, 'core' => ['check_callback' => true, 'data' => ['url' => new JsExpression("function (node) {\n return " . Json::encode(Url::to($this->routes['getTree'])) . ";\n }"), 'data' => new JsExpression("function (node) {\n return { 'id' : node.id };\n }")], 'multiple' => $this->multiple], 'checkbox' => ['three_state' => false], 'contextmenu' => ['items' => $items], 'dnd' => ['is_draggable' => false]];
if (count($this->types) > 0) {
$options['types'] = $this->types;
}
$this->selectOptions['id'] = $id . '-select';
return $this->render('JSSelectableTree', ['id' => $id, 'flagFieldName' => $this->flagFieldName, 'fieldName' => $this->fieldName, 'model' => $this->model, 'multiple' => $this->multiple, 'options' => Json::encode($options), 'routes' => $this->routes, 'selectedItems' => $this->selectedItems, 'selectLabel' => $this->selectLabel, 'selectLabelOptions' => $this->selectLabelOptions, 'selectOptions' => $this->selectOptions, 'selectParents' => $this->selectParents]);
}
示例2: run
/**
* @return string
*/
public function run()
{
parent::run();
if (null === $this->rootCategory) {
return '';
}
$cacheKey = $this->className() . ':' . implode('_', [$this->viewFile, $this->rootCategory, null === $this->depth ? 'null' : intval($this->depth), intval($this->includeRoot), intval($this->fetchModels), intval($this->onlyNonEmpty), implode(',', $this->excludedCategories), \Yii::$app->request->url]) . ':' . json_encode($this->additional);
if (false !== ($cache = \Yii::$app->cache->get($cacheKey))) {
return $cache;
}
/** @var array|Category[] $tree */
$tree = Category::getMenuItems(intval($this->rootCategory), $this->depth, boolval($this->fetchModels));
if (true === $this->includeRoot) {
if (null !== ($_root = Category::findById(intval($this->rootCategory)))) {
$tree = [['label' => $_root->name, 'url' => Url::toRoute(['@category', 'category_group_id' => $_root->category_group_id, 'last_category_id' => $_root->id]), 'id' => $_root->id, 'model' => $this->fetchModels ? $_root : null, 'items' => $tree]];
}
}
if (true === $this->onlyNonEmpty) {
$_sq1 = (new Query())->select('main_category_id')->distinct()->from(Product::tableName());
$_sq2 = (new Query())->select('category_id')->distinct()->from('{{%product_category}}');
$_query = (new Query())->select('id')->from(Category::tableName())->andWhere(['not in', 'id', $_sq1])->andWhere(['not in', 'id', $_sq2])->all();
$this->excludedCategories = array_merge($this->excludedCategories, array_column($_query, 'id'));
}
$tree = $this->filterTree($tree);
$cache = $this->render($this->viewFile, ['tree' => $tree, 'fetchModels' => $this->fetchModels, 'additional' => $this->additional, 'activeClass' => $this->activeClass, 'activateParents' => $this->activateParents]);
\Yii::$app->cache->set($cacheKey, $cache, 0, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(Category::className()), ActiveRecordHelper::getCommonTag(Product::className())]]));
return $cache;
}
示例3: appendPart
/**
* @inheritdoc
*/
public function appendPart($route, $parameters = [], &$used_params = [], &$cacheTags = [])
{
/*
В parameters должен храниться last_category_id
Если его нет - используем parameters.category_id
*/
$category_id = null;
if (isset($parameters['last_category_id'])) {
$category_id = $parameters['last_category_id'];
} elseif (isset($parameters['category_id'])) {
$category_id = $parameters['category_id'];
}
$used_params[] = 'last_category_id';
$used_params[] = 'category_id';
if ($category_id === null) {
return false;
}
$category = Category::findById($category_id);
if (is_object($category) === true) {
$parentIds = $category->getParentIds();
foreach ($parentIds as $id) {
$cacheTags[] = ActiveRecordHelper::getObjectTag(Category::className(), $id);
}
$cacheTags[] = ActiveRecordHelper::getObjectTag(Category::className(), $category_id);
return $category->getUrlPath($this->include_root_category);
} else {
return false;
}
}
示例4: actionIndex
public function actionIndex($category_id = null)
{
$searchModel = new Category();
$searchModel->active = 1;
$params = Yii::$app->request->get();
$dataProvider = $searchModel->search($params);
$selectedCategory = null;
if ($category_id !== null) {
$selectedCategory = Category::findById($category_id);
}
if ($selectedCategory !== null) {
if (Yii::$app->request->isPost === true) {
$newProperty = isset($_GET['add_property_id']) ? Property::findById($_GET['add_property_id']) : null;
if ($newProperty !== null) {
$filterSet = new FilterSets();
$filterSet->category_id = $selectedCategory->id;
$filterSet->property_id = $newProperty->id;
$filterSet->sort_order = 65535;
$filterSet->save();
}
}
}
$groups = PropertyGroup::getForObjectId(Object::getForClass(Product::className())->id, false);
$propertiesDropdownItems = [];
foreach ($groups as $group) {
$item = ['label' => $group->name, 'url' => '#', 'items' => []];
$properties = Property::getForGroupId($group->id);
foreach ($properties as $prop) {
$item['items'][] = ['label' => $prop->name, 'url' => '?category_id=' . $category_id . '&add_property_id=' . $prop->id, 'linkOptions' => ['class' => 'add-property-to-filter-set', 'data-property-id' => $prop->id, 'data-action' => 'post']];
}
$propertiesDropdownItems[] = $item;
}
return $this->render('index', ['dataProvider' => $dataProvider, 'searchModel' => $searchModel, 'selectedCategory' => $selectedCategory, 'propertiesDropdownItems' => $propertiesDropdownItems]);
}
示例5: getParent
/**
* @return ActiveRecord
*/
public function getParent()
{
$cacheKey = 'TreeParent:' . $this->owner->className() . ':' . $this->owner->getAttribute($this->idAttribute);
/** @var $parent ActiveRecord */
$parent = Yii::$app->cache->get($cacheKey);
if ($parent === false) {
$className = $this->owner->className();
$parent = new $className();
$parent_id = $this->owner->getAttribute($this->parentIdAttribute);
if ($parent_id < 1) {
return null;
}
/* findById does not have a calling standard and uses different parameters. For example isActive = 1.
* But we must get a parent model by primary id only.
* Uncomment this code if you unify findById method.
*/
// if ($parent->hasMethod('findById')) {
// $parent = $parent->findById($parent_id);
// } else {
if ($parent instanceof Category) {
$parent = Category::findById($parent_id, null);
} else {
$parent = $parent->findOne($parent_id);
}
Yii::$app->cache->set($cacheKey, $parent, 86400, new TagDependency(['tags' => [\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($className)]]));
}
return $parent;
}
示例6: getCategoryLinks
protected function getCategoryLinks($id)
{
$model = Category::findById($id, null);
if (!is_null($model)) {
$this->frontendLink = Html::a($model->name, ['@category', 'model' => $model, 'category_group_id' => isset($model->category) ? $model->category->category_group_id : null]);
$this->backendLink = Html::a($model->name, ['/shop/backend-category/edit', 'id' => $model->id, 'parent_id' => $model->parent_id, 'returnUrl' => Helper::getReturnUrl()]);
}
}
示例7: getForCategoryId
/**
* @param $categoryId
* @return FilterSets[]
*/
public static function getForCategoryId($categoryId)
{
Yii::beginProfile('FilterSets.GetForCategory ' . $categoryId);
$category = Category::findById($categoryId);
if ($category === null) {
return [];
}
$categoryIds = $category->getParentIds();
$filter_sets = FilterSets::find()->where(['in', 'category_id', $categoryIds])->andWhere(['delegate_to_children' => 1])->orWhere(['category_id' => $category->id])->orderBy(['sort_order' => SORT_ASC])->all();
Yii::endProfile('FilterSets.GetForCategory ' . $categoryId);
return $filter_sets;
}
示例8: getForCategoryId
/**
* @param $categoryId
* @return FilterSets[]
*/
public static function getForCategoryId($categoryId)
{
Yii::beginProfile('FilterSets.GetForCategory ' . $categoryId);
$category = Category::findById($categoryId);
if ($category === null) {
return [];
}
$categoryIds = $category->getParentIds();
$filter_sets = Yii::$app->db->cache(function ($db) use($categoryIds, $category) {
return FilterSets::find()->where(['in', 'category_id', $categoryIds])->andWhere(['delegate_to_children' => 1])->orWhere(['category_id' => $category->id])->orderBy(['sort_order' => SORT_ASC])->all();
}, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(static::className())]]));
Yii::endProfile('FilterSets.GetForCategory ' . $categoryId);
return $filter_sets;
}
示例9: run
public function run()
{
app\backend\assets\FrontendEditingAsset::register($this->view);
$items = [['label' => Icon::show('dashboard') . ' ' . Yii::t('app', 'Backend'), 'url' => ['/backend/']]];
switch (Yii::$app->requestedRoute) {
case 'shop/product/list':
if (isset($_GET['properties'])) {
$apply_if_params = [];
foreach ($_GET['properties'] as $property_id => $values) {
if (isset($values[0])) {
$apply_if_params[$property_id] = $values[0];
}
}
if (Yii::$app->response->dynamic_content_trait === true) {
$items[] = ['label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Edit Dynamic Content'), 'url' => ['/backend/dynamic-content/edit', 'id' => Yii::$app->response->matched_dynamic_content_trait_model->id]];
} else {
if (isset($_GET['properties'], $_GET['last_category_id'])) {
$items[] = ['label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Add Dynamic Content'), 'url' => ['/backend/dynamic-content/edit', 'DynamicContent' => ['apply_if_params' => Json::encode($apply_if_params), 'apply_if_last_category_id' => $_GET['last_category_id'], 'object_id' => Object::getForClass(app\modules\shop\models\Product::className())->id, 'route' => 'shop/product/list']]];
}
}
} else {
// no properties selected - go to category edit page
if (isset($_GET['last_category_id'])) {
$reviewsLink = $this->getReviewEditParams("Category", intval($_GET['last_category_id']));
$cat = app\modules\shop\models\Category::findById($_GET['last_category_id']);
$items[] = ['label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit category'), 'url' => ['/shop/backend-category/edit', 'id' => $cat->id, 'parent_id' => $cat->parent_id]];
}
}
break;
case 'shop/product/show':
if (isset($_GET['model_id'])) {
$reviewsLink = $this->getReviewEditParams("Product", intval($_GET['model_id']));
$items[] = ['label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit product'), 'url' => ['/shop/backend-product/edit', 'id' => intval($_GET['model_id'])]];
}
break;
case '/page/page/show':
case '/page/page/list':
if (isset($_GET['id'])) {
$page = app\modules\page\models\Page::findById($_GET['id']);
$reviewsLink = $this->getReviewEditParams("Page", $_GET['id']);
$items[] = ['label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit page'), 'url' => ['/page/backend/edit', 'id' => $page->id, 'parent_id' => $page->parent_id]];
}
break;
}
if (!empty($reviewsLink)) {
$items[] = $reviewsLink;
}
return $this->render('floating-panel', ['items' => $items, 'bottom' => $this->bottom]);
}
示例10: appendPart
public function appendPart($route, $parameters = [], &$used_params = [], &$cacheTags = [])
{
$used_params[] = 'categories';
$used_params[] = 'category_group_id';
$used_params[] = $this->model_category_attribute;
$attribute_name = $this->model_category_attribute;
$category_id = $this->model->{$attribute_name};
/** @var Category $category */
$category = Category::findById($category_id);
if (is_object($category) === true) {
$parentIds = $category->getParentIds();
foreach ($parentIds as $id) {
$cacheTags[] = ActiveRecordHelper::getObjectTag(Category::className(), $id);
}
$cacheTags[] = ActiveRecordHelper::getObjectTag(Category::className(), $category_id);
return $category->getUrlPath($this->include_root_category);
} else {
return false;
}
}
示例11: JsExpression
?>
</article>
</div>
</section>
<input type="hidden" name="DynamicContent[apply_if_params]" id="apply_if_params">
<?php
BackendWidget::begin(['title' => Yii::t('app', 'Match settings'), 'icon' => 'cogs', 'footer' => $this->blocks['submit']]);
?>
<div id="properties">
<?php
$url = Url::to(['/shop/backend-category/autocomplete']);
$category = $model->apply_if_last_category_id > 0 ? \app\modules\shop\models\Category::findById($model->apply_if_last_category_id) : null;
?>
<?php
echo $form->field($model, 'apply_if_last_category_id')->widget(Select2::classname(), ['options' => ['placeholder' => 'Search for a category ...'], 'pluginOptions' => ['allowClear' => true, 'ajax' => ['url' => $url, 'dataType' => 'json', 'data' => new JsExpression('function(term,page) { return {search:term}; }'), 'results' => new JsExpression('function(data,page) { return {results:data.results}; }')]], 'initValueText' => !is_null($category) ? $category->name : '']);
?>
<div class="row">
<div class="col-md-10 col-md-offset-2">
<a href="#" class="btn btn-md btn-primary add-property">
<?php
echo Icon::show('plus');
?>
<?php
echo Yii::t('app', 'Add property');
?>
</a>
<br>
示例12:
use app\modules\shop\controllers\BackendCategoryController;
use kartik\helpers\Html;
use kartik\icons\Icon;
use app\backend\components\ActiveForm;
use kartik\widgets\DateTimePicker;
use yii\helpers\ArrayHelper;
use app\backend\components\Helper;
use app\modules\shop\ShopModule;
use yii\helpers\Url;
/**
* @var $this \yii\web\View
* @var $model \app\modules\shop\models\Category
*/
$this->title = Yii::t('app', 'Category edit');
$this->params['breadcrumbs'][] = ['url' => ['index'], 'label' => Yii::t('app', 'Categories')];
if ($model->parent_id > 0 && null !== ($parent = \app\modules\shop\models\Category::findById($model->parent_id, null, null))) {
$this->params['breadcrumbs'][] = ['url' => ['index', 'id' => $parent->id, 'parent_id' => $parent->parent_id], 'label' => $parent->name];
}
$this->params['breadcrumbs'][] = $this->title;
?>
<?php
echo app\widgets\Alert::widget(['id' => 'alert']);
?>
<?php
$form = ActiveForm::begin(['id' => 'category-form', 'type' => ActiveForm::TYPE_HORIZONTAL]);
?>
<?php
$this->beginBlock('submit');
示例13: getMainCategory
/**
* Returns product main category model instance using per-request Identity Map
* @return Category|null
*/
public function getMainCategory()
{
return Category::findById($this->main_category_id, null);
}
示例14:
?>
</td>
<td>
<?php
echo Html::checkbox('delegateToChildren[' . $filterSet->id . ']', $filterSet->delegate_to_children === 1, ['class' => 'delegate-property-checkbox', 'data-filterset-id' => $filterSet->id, 'data-inherited' => $filterSet->category_id === $selectedCategory->id ? '0' : '1']);
?>
</td>
<td>
<?php
echo Html::checkbox('isRangeSlider[' . $filterSet->id . ']', $filterSet->is_range_slider === 1, ['class' => 'is-range-slider-checkbox', 'data-filterset-id' => $filterSet->id]);
?>
</td>
<td>
<?php
if ($filterSet->category_id !== $selectedCategory->id) {
$cat = \app\modules\shop\models\Category::findById($filterSet->category_id);
echo Html::a($cat->name, ['/shop/backend-filter-sets/index', 'category_id' => $cat->id]);
} else {
echo '-';
}
?>
</td>
<td>
<?php
echo Html::a(Icon::show('trash-o'), ['/shop/backend-filter-sets/delete-filter-set', 'id' => $filterSet->id, 'category_id' => $selectedCategory->id], ['class' => 'btn btn-danger btn-xs', 'data-action' => 'post']);
?>
<?php
if ($property->has_static_values === 1) {
?>
<a href="#psv-table-<?php
echo $property->id;
示例15: actionDelete
public function actionDelete($id = null, $parent_id = null, $mode = null)
{
if (null === $id || null === ($model = Category::findById($id, null, null))) {
throw new NotFoundHttpException();
}
$model->deleteMode = $mode;
if (!$model->delete()) {
Yii::$app->session->setFlash('success', Yii::t('app', 'The object is placed in the cart'));
} else {
Yii::$app->session->setFlash('success', Yii::t('app', 'Object has been removed'));
}
return $this->redirect(Yii::$app->request->get('returnUrl', Url::to(['index', 'parent_id' => $model->parent_id])));
}