public function actionSave($languageId = null, $categoryId = null) { if (!empty($categoryId)) { $category = Category::findOne($categoryId); $category_translation = CategoryTranslation::find()->where(['category_id' => $categoryId, 'language_id' => $languageId])->one(); if (empty($category_translation)) { $category_translation = new CategoryTranslation(); } } else { $category = new Category(); $category_translation = new CategoryTranslation(); } if (Yii::$app->request->isPost) { $category->load(Yii::$app->request->post()); $category_translation->load(Yii::$app->request->post()); if ($category->validate() && $category_translation->validate()) { $category->save(); $category_translation->category_id = $category->id; $category_translation->language_id = $languageId; $category_translation->save(); Yii::$app->getSession()->setFlash('success', 'Data were successfully modified.'); return $this->redirect(Url::toRoute('/articles/category')); } else { Yii::$app->getSession()->setFlash('danger', 'Failed to change the record.'); } } return $this->render('edit', ['category' => $category, 'category_translation' => $category_translation, 'categories' => Category::find()->with('translations')->all(), 'selectedLanguage' => Language::findOne($languageId), 'languages' => Language::findAll(['active' => true])]); }
public static function getOneCategory($id) { $model = CategoryTranslation::find(); if (!empty($id)) { $model->andWhere(['category_id' => $id]); } return $model->one(); }
private function findCategoryBySeoUrl($seoUrl, $parentId, $options = []) { $categoriesSeoData = SeoData::find()->where(['entity_name' => CategoryTranslation::className(), 'seo_url' => $seoUrl])->all(); if ($categoriesSeoData) { foreach ($categoriesSeoData as $categorySeoData) { if ($category = Category::find()->joinWith('translations translation')->where(array_merge(['translation.id' => $categorySeoData->entity_id, 'parent_id' => $parentId, 'translation.language_id' => $this->currentLanguage->id], $options))->one()) { return $category; } } } return null; }
/** * @return \yii\db\ActiveQuery */ public function getTranslations() { return $this->hasMany(CategoryTranslation::className(), ['category_id' => 'id']); }