From e9de2296051b0aeba519a7c19be63c4aca3fc8b5 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 20 Sep 2016 23:02:27 +0100 Subject: [PATCH] Explicitly record ingredient ordering Previous versions relied on ID ordering, which theoretically is safe, so it'll probably break horribly at some point. --- modules/ORB/System/Recipe.pm | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/ORB/System/Recipe.pm b/modules/ORB/System/Recipe.pm index afe7862..5d7d60f 100644 --- a/modules/ORB/System/Recipe.pm +++ b/modules/ORB/System/Recipe.pm @@ -261,15 +261,16 @@ sub _add_recipe_ingredients { # Prepare the add query to make work easier later my $addh = $self -> {"dbh"} -> prepare("INSERT INTO `".$self -> {"settings"} -> {"database"} -> {"recipeing"}."` - (`recipe_id`, `unit_id`, `prep_id`, `ingred_id`, `quantity`, `notes`, `separator`) - VALUES(?, ?, ?, ?, ?, ?, ?)"); + (`recipe_id`, `position`, `unit_id`, `prep_id`, `ingred_id`, `quantity`, `notes`, `separator`) + VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); # Now go through each of the ingredients + my $position = 0; foreach my $ingred (@{$ingredients}) { # Handle serparators... separately if($ingred -> {"separator"}) { - $addh -> execute($recipeid, undef, undef, undef, undef, undef, $ingred -> {"name"}) + $addh -> execute($recipeid, $position, undef, undef, undef, undef, undef, $ingred -> {"name"}) or return $self -> self_error("Error adding separator '".$ingred -> {"name"}."': ".$self -> {"dbh"} -> errstr()); # Otherwise, it's a real ingredient, so we need to do the more complex work @@ -279,12 +280,14 @@ sub _add_recipe_ingredients { or return $self -> self_error("Unable to get ingreditent ID for '".$ingred -> {"name"}."': ".$self -> {"ingredient"} -> errstr()); # If we have an ID we can add the ingredient. - $addh -> execute($recipeid, $ingred -> {"quant"}, $ingred -> {"units"}, $ingred -> {"prepid"}, $ingid, $ingred -> {"notes"}, undef) + $addh -> execute($recipeid, $position, $ingred -> {"units"}, $ingred -> {"prepid"}, $ingid, $ingred -> {"quant"}, $ingred -> {"notes"}, undef) or return $self -> self_error("Unable to add ingredient '".$ingred -> {"name"}."' to recipe '$recipeid': ".$self -> {"dbh"} -> errstr()); # And increase the ingredient refcount $self -> {"system"} -> {"ingredients"} -> increase_refcount($ingid); } + + ++$position; } # Get here and all worked out okay