Create models for the recipe app and connect them to the database
This commit is contained in:
parent
28b4eb6127
commit
597d46e2ab
10
apps/Recipes/Controllers/CatalogController.php
Normal file
10
apps/Recipes/Controllers/CatalogController.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lycoreco\Apps\Recipes\Controllers;
|
||||||
|
|
||||||
|
use Lycoreco\Includes\BaseController;
|
||||||
|
|
||||||
|
class CatalogController extends BaseController
|
||||||
|
{
|
||||||
|
protected $template_name = APPS_PATH . '/Recipes/Templates/catalog.php';
|
||||||
|
}
|
||||||
10
apps/Recipes/Controllers/SingleRecipeController.php
Normal file
10
apps/Recipes/Controllers/SingleRecipeController.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lycoreco\Apps\Recipes\Controllers;
|
||||||
|
|
||||||
|
use Lycoreco\Includes\BaseController;
|
||||||
|
|
||||||
|
class SingleRecipeController extends BaseController
|
||||||
|
{
|
||||||
|
protected $template_name = APPS_PATH . '/Recipes/Templates/single.php';
|
||||||
|
}
|
||||||
@ -5,5 +5,20 @@ use Lycoreco\Includes\Model\BaseModel;
|
|||||||
|
|
||||||
class CategoryModel extends BaseModel
|
class CategoryModel extends BaseModel
|
||||||
{
|
{
|
||||||
|
public $field_name;
|
||||||
|
|
||||||
|
static protected $table_name = 'categories';
|
||||||
|
static protected $table_fields = [
|
||||||
|
'id' => 'int',
|
||||||
|
'name' => 'string',
|
||||||
|
];
|
||||||
|
public static function init_table()
|
||||||
|
{
|
||||||
|
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
|
||||||
|
name VARCHAR(255) NOT NULL
|
||||||
|
);');
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -5,5 +5,29 @@ use Lycoreco\Includes\Model\BaseModel;
|
|||||||
|
|
||||||
class IngredientInRecipeModel extends BaseModel
|
class IngredientInRecipeModel extends BaseModel
|
||||||
{
|
{
|
||||||
|
public $field_ingredient_id;
|
||||||
|
public $field_receipt_id;
|
||||||
|
public $field_amount;
|
||||||
|
|
||||||
|
static protected $table_name = 'recipe_ingredients';
|
||||||
|
static protected $table_fields = [
|
||||||
|
'id' => 'int',
|
||||||
|
'ingredient_id' => 'int',
|
||||||
|
'receipt_id' => 'int',
|
||||||
|
'amount' => 'int'
|
||||||
|
];
|
||||||
|
public static function init_table()
|
||||||
|
{
|
||||||
|
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
|
||||||
|
ingredient_id INT NOT NULL,
|
||||||
|
receipt_id INT NOT NULL,
|
||||||
|
amount INT NOT NULL,
|
||||||
|
|
||||||
|
FOREIGN KEY (ingredient_id) REFERENCES ingredients(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (receipt_id) REFERENCES recipes(id) ON DELETE CASCADE
|
||||||
|
);');
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -5,5 +5,23 @@ use Lycoreco\Includes\Model\BaseModel;
|
|||||||
|
|
||||||
class IngredientModel extends BaseModel
|
class IngredientModel extends BaseModel
|
||||||
{
|
{
|
||||||
|
public $field_name;
|
||||||
|
public $field_unit_name;
|
||||||
|
static protected $table_name = 'ingredients';
|
||||||
|
|
||||||
|
static protected $table_fields = [
|
||||||
|
'id' => 'int',
|
||||||
|
'name' => 'string',
|
||||||
|
'unit_name' => 'string'
|
||||||
|
];
|
||||||
|
public static function init_table()
|
||||||
|
{
|
||||||
|
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
unit_name VARCHAR(20) NULL
|
||||||
|
);');
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -13,4 +13,33 @@ class RecipeModel extends BaseModel
|
|||||||
public $field_author_id;
|
public $field_author_id;
|
||||||
public $field_status;
|
public $field_status;
|
||||||
public $field_created_at;
|
public $field_created_at;
|
||||||
|
|
||||||
|
static protected $table_name = 'recipes';
|
||||||
|
static protected $table_fields = [
|
||||||
|
'id' => 'int',
|
||||||
|
'title' => 'string',
|
||||||
|
'instruction' => 'string',
|
||||||
|
'estimated_time' => 'int',
|
||||||
|
'estimated_price' => 'int',
|
||||||
|
'author_id' => 'int',
|
||||||
|
'status' => 'string',
|
||||||
|
'created_at' => 'DateTime'
|
||||||
|
];
|
||||||
|
public static function init_table()
|
||||||
|
{
|
||||||
|
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
|
||||||
|
title VARCHAR(255) NOT NULL,
|
||||||
|
instruction TEXT NOT NULL,
|
||||||
|
estimated_time INT NOT NULL,
|
||||||
|
estimated_price INT NOT NULL,
|
||||||
|
author_id INT NOT NULL,
|
||||||
|
status VARCHAR(20) NOT NULL CHECK (status IN (\'publish\', \'pending\')),
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
|
FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE CASCADE
|
||||||
|
);');
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
1
apps/Recipes/Templates/catalog.php
Normal file
1
apps/Recipes/Templates/catalog.php
Normal file
@ -0,0 +1 @@
|
|||||||
|
Catalog page
|
||||||
1
apps/Recipes/Templates/single.php
Normal file
1
apps/Recipes/Templates/single.php
Normal file
@ -0,0 +1 @@
|
|||||||
|
Single Recipe item
|
||||||
@ -1,3 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Lycoreco\Includes\Routing\Path;
|
use Lycoreco\Apps\Recipes\Controllers;
|
||||||
|
use Lycoreco\Includes\Routing\Path;
|
||||||
|
|
||||||
|
$recipes_urls = [
|
||||||
|
new Path('/recipe/[:string]', new Controllers\SingleRecipeController(), 'single'),
|
||||||
|
new Path('/catalog', new Controllers\CatalogController(), 'catalog'),
|
||||||
|
];
|
||||||
|
|||||||
2
urls.php
2
urls.php
@ -5,11 +5,13 @@ require APPS_PATH . '/Index/urls.php';
|
|||||||
require APPS_PATH . '/Users/urls.php';
|
require APPS_PATH . '/Users/urls.php';
|
||||||
require APPS_PATH . '/Admin/urls.php';
|
require APPS_PATH . '/Admin/urls.php';
|
||||||
require APPS_PATH . '/Ajax/urls.php';
|
require APPS_PATH . '/Ajax/urls.php';
|
||||||
|
require APPS_PATH . '/Recipes/urls.php';
|
||||||
|
|
||||||
Router::includes($index_urls, "index");
|
Router::includes($index_urls, "index");
|
||||||
Router::includes($users_urls, 'users');
|
Router::includes($users_urls, 'users');
|
||||||
Router::includes($admin_urls, 'admin');
|
Router::includes($admin_urls, 'admin');
|
||||||
Router::includes($ajax_urls, 'ajax');
|
Router::includes($ajax_urls, 'ajax');
|
||||||
|
Router::includes($recipes_urls, 'recipes');
|
||||||
|
|
||||||
// Router::set_error_controller('default', new ErrorController())
|
// Router::set_error_controller('default', new ErrorController())
|
||||||
?>
|
?>
|
||||||
Loading…
x
Reference in New Issue
Block a user