Merge pull request 'TIST-35: Favorite property on ProductModel' (#26) from TIST-35 into develop

Reviewed-on: #26
Reviewed-by: greendavid004 <davidkatrinka1995@gmail.com>
This commit is contained in:
steve_dekart 2025-07-02 20:45:28 +02:00
commit cc0125d997

View File

@ -16,6 +16,7 @@ class RecipeModel extends BaseModel
public $field_created_at;
public $category_name;
public $is_in_favorite = 0;
const STATUS = [['publish', 'Publish'], ['pending', 'Pending']];
@ -48,6 +49,23 @@ class RecipeModel extends BaseModel
'status' => 'string',
'created_at' => 'DateTime'
];
protected static function get_additional_fields(){
$add_fields = parent::get_additional_fields();
// If user is authorized, we also check product in thw wishlist
if(CURRENT_USER) {
$add_fields = array_merge($add_fields, array(
[
"field" => [
"MAX(CASE WHEN fav.user_id = ". CURRENT_USER->get_id() ." THEN 1 ELSE 0 END) AS is_in_favorite"
],
"join_table" => "recipe_favorites fav ON fav.recipe_id = obj.id"
]
));
}
return $add_fields;
}
public static function init_table()
{
$result = db_query('CREATE TABLE ' . static::$table_name . ' (