diff --git a/apps/Recipes/Controllers/SingleRecipeController.php b/apps/Recipes/Controllers/SingleRecipeController.php index d74d230..aa883a2 100644 --- a/apps/Recipes/Controllers/SingleRecipeController.php +++ b/apps/Recipes/Controllers/SingleRecipeController.php @@ -2,9 +2,40 @@ namespace Lycoreco\Apps\Recipes\Controllers; +use Lycoreco\Apps\Recipes\Models\RecipeModel; use Lycoreco\Includes\BaseController; +use Lycoreco\Includes\Routing\HttpExceptions; class SingleRecipeController extends BaseController { protected $template_name = APPS_PATH . '/Recipes/Templates/single.php'; -} \ No newline at end of file + + protected function get_model() + { + if (isset($this->__model)) + return $this->__model; + + $this->__model = RecipeModel::get( + array( + [ + 'name' => 'obj.id', + 'type' => '=', + 'value' => $this->url_context['url_1'] + ] + ) + ); + if (empty($this->__model)) + throw new HttpExceptions\NotFound404('Recipe not found'); + + return $this->__model; + } + + public function get_context_data() + { + $context = parent::get_context_data(); + + $context['recipe'] = $this->get_model(); + + return $context; + } +} diff --git a/apps/Recipes/Models/RecipeModel.php b/apps/Recipes/Models/RecipeModel.php index 67ea052..9d69626 100644 --- a/apps/Recipes/Models/RecipeModel.php +++ b/apps/Recipes/Models/RecipeModel.php @@ -15,10 +15,27 @@ class RecipeModel extends BaseModel public $field_status; public $field_created_at; + public $category_name; + const STATUS = [[ 'publish', 'Publish' ], [ 'pending', 'Pending' ]]; static protected $search_fields = ['obj.title']; static protected $table_name = 'recipes'; + + static protected $additional_fields = array( + [ + 'field' => [ + 'tb1.name AS category_name' + ], + 'join_table' => 'ingredients tb1 ON tb1.id = obj.category_id' + ], + [ + 'field' => [ + + ], + 'join_table' => 'recipe_ingredients tb2 ON tb2.recipe_id = obj.id' + ] + ); static protected $table_fields = [ 'id' => 'int', 'title' => 'string', diff --git a/apps/Recipes/Templates/single.php b/apps/Recipes/Templates/single.php index 10763d4..4300772 100644 --- a/apps/Recipes/Templates/single.php +++ b/apps/Recipes/Templates/single.php @@ -1 +1,3 @@ -Single Recipe item \ No newline at end of file +
+
+
\ No newline at end of file diff --git a/apps/Recipes/urls.php b/apps/Recipes/urls.php index 15db150..1445dc8 100644 --- a/apps/Recipes/urls.php +++ b/apps/Recipes/urls.php @@ -4,6 +4,6 @@ use Lycoreco\Apps\Recipes\Controllers; use Lycoreco\Includes\Routing\Path; $recipes_urls = [ - new Path('/recipe/[:string]', new Controllers\SingleRecipeController(), 'single'), + new Path('/recipe/[:int]', new Controllers\SingleRecipeController(), 'single'), new Path('/catalog', new Controllers\CatalogController(), 'catalog'), ];