public function addRecipe($book_id, $recipe_id)
 {
     $book = RecipeBook::find($book_id);
     $recipe = Recipe::find($recipe_id);
     $recipe->recipeBooks()->attach($book->id);
     return $recipe;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $this->addParam('categoryId', $params);
     $caloriesSql = self::getCaloriesQuery('`r`.`id`')->createCommand()->sql;
     $proteinsSql = self::getProteinsQuery('`r`.`id`')->createCommand()->sql;
     $fatsSql = self::getFatsQuery('`r`.`id`')->createCommand()->sql;
     $carbohydratesSql = self::getCarbohydratesQuery('`r`.`id`')->createCommand()->sql;
     $query = Recipe::find()->select(['r.*', 'categoryName' => 'c.name', 'calories' => "({$caloriesSql})", 'proteins' => "({$proteinsSql})", 'fats' => "({$fatsSql})", 'carbohydrates' => "({$carbohydratesSql})"])->from(self::tableName() . ' `r`')->leftJoin(RecipeCategory::tableName() . ' `c`', '`c`.`id` = `r`.`category_id`');
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere([]);
     $query->andFilterWhere(['like', '`r`.`name`', $this->name])->andFilterWhere(['like', '`r`.`description`', $this->description])->andFilterWhere(['like', '`c`.`name`', $this->categoryName])->andFilterWhere(['category_id' => $this->categoryId]);
     $dataProvider->sort = ['attributes' => ['name', 'categoryName', 'calories', 'proteins', 'fats', 'carbohydrates']];
     return $dataProvider;
 }
 /**
  * @return array
  */
 public function getRecipeItems()
 {
     if ($this->_recipeItems !== null) {
         return $this->_recipeItems;
     }
     $this->_recipeItems = [0 => '---'];
     $query = Recipe::find()->orderBy('name');
     if (!empty($this->recipeCategoryId)) {
         $query->where(['category_id' => $this->recipeCategoryId]);
     }
     $items = $query->asArray()->all();
     if (!empty($items)) {
         foreach ($items as $item) {
             $this->_recipeItems[$item['id']] = $item['name'];
         }
     }
     return $this->_recipeItems;
 }
 public function destroy($id)
 {
     $recipe = Recipe::find($id);
     $recipe->delete();
     return $recipe;
 }