Commit f6ab012c by Luis Perez

Añadido nuevo metodo al modelo que devuelve las recetas de la semana.

Nuevo elemento SQL para cargar
parent 5f2942dc
ALTER TABLE `ingredients`
ADD COLUMN `desc` varchar(255) NULL AFTER `id`;
......@@ -13,8 +13,8 @@ use Orm\Model;
* @license SETI Consultyn Software License <license.txt>
* @link http://seticonsultyn.es
*/
class Model_Recipe extends Model{
use \Trait_InutraliaWS;
protected static $_table_name = 'recipes';
......@@ -24,10 +24,75 @@ class Model_Recipe extends Model{
'ingredients' => array(
'key_from' => 'id',
'key_to' => 'recipe_local_id',
'model_to' => 'Model_Ingredients',
'cascade_delete' => false
'model_to' => 'Model_Ingredient',
'cascade_delete' => false,
'conditions' => array(
'where' => array('used' => 0),
'order_by' => array('used' => 'DESC'),
'group_by' => 'ingredient_id'
)
),
);
public static function get_recipes_current_week()
{
//Devolvemos recetas guardadas en la base de datos que correspondan a esta vista.
return self::recipes_current_week()->get();
}
public static function recipes_current_week()
{
//Devolvemos recetas guardadas en la base de datos que correspondan a esta vista.
return self::query()->where('week_year', date('W'))->where('year', date('Y'))->where('user_id', \Fuel\Core\Session::get('user_id'));
}
public function load_ingredients()
{
if($this->loaded=='S' || count($this->ingredients)>0 )
{
return false;
}
try
{
\DB::start_transaction();
$recipe = $this->ws_recipe_get($this->recipe_id);
foreach ($recipe->ingredients as $ingredientRecipe)
{
$ingredient = Model_Ingredient::forge();
$ingredient->ingredient_id = $ingredientRecipe->id;
$ingredient->desc = $ingredientRecipe->name;
$ingredient->used = 'N';
$ingredient->created_at = date("Y-m-d H:i:s");
$this->ingredients[] = $ingredient;
}
$this->loaded = 'S';
$this->save(true);
}
catch (Exception $e)
{
Log::error($e->getMessage());
\DB::rollback_transaction();
return false;
}
\DB::commit_transaction();
return $this->ingredients;
}
}
\ No newline at end of file
......@@ -128,8 +128,8 @@ trait Trait_InutraliaWS {
}
public function ws_recipe_get($id = null){
public function ws_recipe_get($id = null)
{
return $this->ws_get('recipe'.(($id!=null)?"/$id":""));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment