public function testGetPostsWithCategory() { $category = $this->categoryModel->findOne(1); $expectedPosts = $category->getPosts(); $actualPosts = $this->postModel->findAll(['category_id' => 1, 'publish_status' => Post::STATUS_PUBLISH]); $this->assertEquals($expectedPosts->count, count($actualPosts)); }
/** * 其他分类需要显示在导航栏的新增方法 * @param string $ctype 其他分类表标识 a:文章分类表,c:商品分类表 * @param int $cid 其他表分类id * @return mixed */ public function addData($ctype, $cid) { // 如果已经存在就不继续执行了 if ($this->find()->where(['ctype' => $ctype, 'cid' => $cid])->one()) { return true; } switch ($ctype) { case 'a': // 文章分类 $this->name = ArticleCat::findOne($cid)->cat_name; $this->url = ''; // 前台还没有做,暂时空起 break; case 'c': // 商品分类 $this->name = Category::findOne($cid)->cat_name; $this->url = ''; // 前台还没有做,暂时空起 break; default: return false; } $this->ctype = $ctype; $this->cid = $cid; $this->type = 'middle'; if ($this->insert(false)) { return true; } return false; }
public function actionLoadGoods($id, $category = 'all', $q = '') { $offset = (int) Yii::$app->request->post('offset'); Yii::$app->response->format = Response::FORMAT_JSON; /* @var $model Store */ $model = Store::findOne(['id' => $id, 'status' => [Store::STATUS_ACTIVE, Store::STATUS_REST]]); if (!$model) { throw new NotFoundHttpException('未找到该营业点!'); } if ($q !== '') { $query = $model->getGoods()->andWhere(['or', ['like', 'name', $q], ['like', 'description', $q]]); } else { $modelCate = $category === 'all' ? null : Category::findOne(['slug' => $category]); if ($modelCate) { $query = $model->getGoods($modelCate->id); } else { $query = $model->getGoods(); } } $limit = 8; $goodsList = $query->offset($offset)->limit($limit)->all(); $output = ['status' => 'ok', 'html' => '', 'length' => count($goodsList)]; $output['end'] = $output['length'] < $limit; foreach ($goodsList as $goods) { $output['html'] .= $this->renderPartial('_item', ['goods' => $goods, 'lazy' => false]); } return $output; }
public function actionIndex($slug) { $query = Vidmage::find()->joinWith('vidmageCategories.category')->where(['category.slug' => $slug])->orderBy(['id' => SORT_DESC]); $vidmages = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); $channel = Category::findOne(['slug' => $slug]); return $this->render('index', compact('vidmages', 'channel')); }
public function actionCategory($id) { $backUrl = \Yii::$app->request->referrer; $category = Category::findOne($id); $this->layout = 'basic-category'; return $this->render('category', compact('category', 'backUrl')); }
protected function findById($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('请求页面不存在'); } }
/** * Возвращает модель категории. * @param int $id идентификатор категории * @throws NotFoundHttpException в случае, когда категория не найдена * @return Category */ public function getCategory($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested post does not exist.'); } }
/** * Finds the Category model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Category the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionDelete() { /** * @var $model Category */ $model = Category::findOne(Yii::$app->request->post('id')); $model->delete(); }
/** * Finds the Category model based on its slug. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $slug * @return Category the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findCategory($slug) { if (($model = Category::findOne(['slug' => $slug])) !== null) { return $model; } else { throw new NotFoundHttpException(Yii::t('frontend', 'The requested page does not exist.')); } }
/** * Action method for viewing a single forum category. * @param integer $id of the forum category to be shown. * @param string|null $slug of the forum category to be shown. Default value is `null` meaning the requested topic * has no set slug. * @return string|Response action method execution result. * @throws HttpException in case category cannot be found. */ public function actionView($id, $slug = null) { $category = Category::findOne($slug === null ? $id : ['id' => $id, 'slug' => $slug]); if ($category === null) { throw new HttpException(404, 'Cannot find requested forum category!'); } return $this->render('view', ['category' => $category]); }
public function getCategory() { $post = Post::findOne($this->id); foreach (explode(',', $post->category_id) as $value) { $category = Category::findOne($value); $data[] = ['id' => $category->id, 'parent_id' => $category->parent_id, 'title' => $category->title, 'indent' => $this->getIndent($category->indent)]; } return $data; }
public function actionCategory($url) { $category = Category::findOne(['url' => $url]); if (empty($category)) { throw new BadRequestHttpException('Category Not Found!', 404); } $posts = Post::find()->where(['category_id' => $category['id']])->with('category')->all(); return $this->render('list', ['posts' => $posts]); }
public function actionCategory($id = null) { $category = Category::findOne(['id' => $id]); if ($category === NULL) { throw new NotFoundHttpException("Категория {$id} не найдена"); } $this->getView()->title = "Mexanika 74 - Каталог техники: категория " . $category->name; return $this->render('category_item', ['category' => $category]); }
public function actionRenderpage($url) { $category = Category::findOne(['link' => $url]); if (empty($category)) { return $this->run('site/error'); } $subcategories = Category::findAll(['parent' => $category->id]); $posts = Post::findAll(['category' => $category->id]); return $this->render('category', ['category' => $category, 'subcategories' => $subcategories, 'postsCount' => sizeof($posts), 'posts' => $posts, 'premiumPosts' => \common\models\Post::find()->where(['>', 'premium', date('d-m-Y H:i:s')])->andWhere(['category' => $category->id])->all()]); }
public function actionEditcategory($id) { $category = Category::findOne(['id' => $id]); if (!$category) { return $this->run('error'); } if (\Yii::$app->request->post("Category")) { $category->attributes = \Yii::$app->request->post("Category"); $category->save(); } return $this->render('category_edit', ['category' => $category, 'parents' => Category::getList()]); }
public function up() { $this->createTable("{{%{$this->closureTbl}}}", ["parent" => Schema::TYPE_INTEGER . " NOT NULL", "child" => Schema::TYPE_INTEGER . " NOT NULL", "depth" => Schema::TYPE_INTEGER . " NOT NULL DEFAULT 0"]); $this->addPrimaryKey("PK_{$this->closureTbl}", "{{%{$this->closureTbl}}}", ["parent", "child"]); $this->createIndex("FK_{$this->closureTbl}_child_{$this->relativeTbl}", "{{%{$this->closureTbl}}}", "child"); $this->addForeignKey("FK_{$this->closureTbl}_child_{$this->relativeTbl}", "{{%{$this->closureTbl}}}", "child", "{{%{$this->relativeTbl}}}", "id", "CASCADE"); $this->addForeignKey("FK_{$this->closureTbl}_parent_{$this->relativeTbl}", "{{%{$this->closureTbl}}}", "parent", "{{%{$this->relativeTbl}}}", "id", "CASCADE"); // Optional, make first category as root category // Pleas delete that or change to your model class and ID // Also you must connect behavior before run migration \common\models\Category::findOne(1)->saveNodeAsRoot(false); }
public function parseRequest($manager, $request) { /* @var Category $category */ $pathInfo = $request->getPathInfo(); if (($pos = strpos($pathInfo, '-')) === false && is_numeric($pathInfo)) { $category = Category::find()->where('id = :id', array(':id' => $pathInfo))->andWhere('slug = "" OR slug IS NULL')->one(); } elseif ($pos !== false && is_numeric($id = substr($pathInfo, 0, $pos))) { $category = Category::findOne(['id' => $id, 'slug' => substr($pathInfo, $pos + 1)]); } if (isset($category)) { return ['category/view', ['id' => $category->id, 'slug' => $category->slug]]; } return false; }
public function actionCategory($id) { $model = Category::findOne($id); $params['model'] = $model; $query = Content::find(); $query->andFilterWhere(['status' => Content::STATUS_PUBLISH, 'category_id' => $model->id]); $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count()]); $pages->pageSize = 5; $params['pages'] = $pages; $models = $query->offset($pages->offset)->limit($pages->limit)->orderBy('id DESC')->all(); $params['models'] = $models; return $this->render("category", $params); }
private static function getProduct($categoryID) { $categoryModel = Category::findOne(['id' => $categoryID])->children()->all(); $categories = array(); $categories[] = $categoryID; foreach ($categoryModel as $category) { $categories[] = $category->id; } $productModel = Product::find()->where(['in', 'category_id', $categories]); $count = $productModel->count(); $pagination = new Pagination(['totalCount' => $count]); $nodes = $productModel->offset($pagination->offset)->limit(18)->orderBy(['id' => SORT_DESC])->all(); return ['nodes' => $nodes, 'pagination' => $pagination]; }
/** * @param \yii\base\Action $action * * @return bool */ public function beforeAction($action) { $request = \Yii::$app->request; if ($request->get('category_id')) { $id = $request->get('category_id'); /** @var Category $category */ $category = Category::findOne($id); if ($category) { /** @var $controller ArticleController */ $controller = $this->owner; $controller->setCategory($category); } } return parent::beforeAction($action); }
public function actionEdit($id) { $model = new CategoryForm(); if (!($desiredModel = Category::findOne($id))) { throw new NotFoundHttpException("Не найдена категория с идентификатором {$id}!"); } $model->loadCategory($desiredModel); if (\Yii::$app->request->post('CategoryForm') && $model->load(\Yii::$app->request->post())) { if ($model->save()) { \Yii::$app->session->setFlash('success', 'Категория успешно отредактирована!'); } else { \Yii::$app->session->setFlash('error', 'Произошла ошибка при редактировании категории!'); } } return $this->render('edit', ['model' => $model]); }
public function actionUpdate($id) { $model = Category::findOne($id); if (!$model) { throw new NotFoundHttpException('未找到该分类。'); } if ($model->load(Yii::$app->request->post())) { if ($model->save()) { Yii::$app->session->setFlash('success', '成功更新分类“' . $model->name . '”。'); return $this->refresh(); } else { Yii::$app->session->setFlash('danger', '分类更新失败。'); } } return $this->render('form', ['model' => $model]); }
public function actionCategory($id) { $category = Category::findOne(['id' => $id]); if ($category === NULL) { throw new NotFoundHttpException("Категория {$id} не найдена"); } $this->getView()->title = "Mexanika 74 - Запчасти: категория " . $category->name; //return $this->render('category_item', ['category'=>$category]); if (!Category::findOne(['parent_id' => $category->id])) { $dataProvider = new ActiveDataProvider(['query' => $category->getParts()]); return $this->render('list', ['dataProvider' => $dataProvider, 'category' => $category]); } else { $partions = Category::getPartions($category->id); return $this->render('index', ['partions' => $partions]); } }
public function actionCategory($id) { $category = Category::findOne($id); if (!$category) { throw new \yii\web\HttpException(400, 'Wrong category id', 405); } $query = Post::find()->where(['category_id' => $category->id]); $count = $query->count(); if ($count == 1) { return $this->redirect(array('posts/view', 'id' => $query->all()[0]->id)); } $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 10]); $pages->pageSizeParam = false; $posts = $query->offset($pages->offset)->limit($pages->limit)->orderBy('id desc')->all(); return $this->render('category', ['category' => $category->title, 'posts' => $posts, 'pagination' => $pages]); }
/** * Updates an existing Post model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $parents = []; $root = Category::findOne(['parent_id' => null, 'title' => 'post', 'module' => 'post']); if (empty($root)) { throw new NotFoundHttpException('Module không tồn tại'); } $parents = $root->children()->all(); $parent_id = $this->buildTree($parents); if ($model->load(Yii::$app->request->post())) { $model['slug'] = $this->slugAlias($model); if ($model->save()) { return $this->redirect(['index']); } } else { return $this->render('update', ['model' => $model, 'parent_id' => $parent_id]); } }
public function run() { parent::run(); // TODO: Change the autogenerated stub $category = new Category(); $product = new Product(); $parent_category = $category->findOne(['id' => $this->category_id]); $categoriesData = $category->findAll(['parent_id' => $this->category_id]); $category_slug = $parent_category->slug; $categoryIDs = []; if ($categoriesData) { foreach ($categoriesData as $categoryData) { $categoryIDs[] = $categoryData->id; } $products = $product->find()->where(['status' => 10, 'is_featured' => 10])->andWhere(['in', 'category_id', $categoryIDs])->limit($this->limit)->all(); } else { $products = $product->find()->where(['status' => 10, 'is_front' => 10, 'category_id' => $this->category_id])->limit($this->limit)->all(); } return $this->render('widget/front_product', ['nodes' => $products, 'category_slug' => $category_slug]); }
/** * Updates an existing Product model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { $model->file = UploadedFile::getInstance($model, 'file'); if ($model->validate()) { $filename = 'uploads/' . md5($model->file->baseName) . '.' . $model->file->extension; if ($model->file->saveAs($filename)) { $model->image = Url::to($filename, true); $model->save(false); foreach ($model->categories as $categoryId) { $category = Category::findOne($categoryId); $model->link('categories', $category); } return $this->redirect(['view', 'id' => $model->id]); } } } return $this->render('update', ['model' => $model]); }
public function actionView($id) { $category = $this->getCategories(); $page = Post::find()->where(['type' => 'page'])->all(); $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { if (!empty($_POST['category'])) { foreach ($_POST['category'] as $value) { $category = Category::findOne($value); $menuitem = new MenuItem(); $menuitem->menu_id = $id; // if (!empty($category->parent_id)) // $menuitem->parent_id = $category->parent_id; $menuitem->type_id = $category->id; $menuitem->type = 'category'; $menuitem->type_name = $category->title; $menuitem->type_slug = $category->slug; $menuitem->order = 0; $menuitem->save(); } return $this->redirect(['view', 'id' => $model->id]); } if (!empty($_POST['page'])) { foreach ($_POST['page'] as $value) { $page = Post::findOne($value); $menuitem = new MenuItem(); $menuitem->menu_id = $id; $menuitem->type_id = $page->id; $menuitem->type = 'page'; $menuitem->type_name = $page->title; $menuitem->type_slug = $page->slug; $menuitem->order = 0; $menuitem->save(); } return $this->redirect(['view', 'id' => $model->id]); } } $menuitem = MenuItem::find()->where(['menu_id' => $id])->orderBy(['order' => SORT_ASC])->all(); $menuall = Menu::find()->all(); return $this->render('view', ['category' => $category, 'page' => $page, 'model' => $model, 'menuitem' => $menuitem, 'menuall' => $menuall]); }
public function actionCategory($id = 0, $name = '') { $is_parent = FALSE; $category = Category::findOne($id); if (empty($category->parent_id)) { $is_parent = TRUE; } if ($is_parent) { $children = Category::find()->getChildCategories($category->id)->all(); $category_ids = array_map(function ($child) { return $child->id; }, $children); $category_ids[] = $category->id; } $query = Product::find(); $query->joinWith(['productCategories']); $query->andWhere(['in', 'product_category.category_id', $category_ids]); $query->andWhere('status = :s AND is_private = :p', [':s' => 1, ':p' => 0]); $query->orderBy('id DESC'); $pageTile = "Items Available in : {$category->display_name}"; $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 12]]); return $this->render('category', ['dataProvider' => $dataProvider, 'pageTile' => $pageTile]); }