Skip to content

Commit

Permalink
Fix for Find by Ingredient
Browse files Browse the repository at this point in the history
  • Loading branch information
nazgul26 committed May 2, 2023
1 parent 6c19ba4 commit 032df4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
36 changes: 17 additions & 19 deletions src/Controller/RecipesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,28 +305,26 @@ public function contains() {
$this->loadModel('IngredientMappings');
if ($this->request->is(array('post', 'put'))) {
$ingredients = $this->request->data;
$results = $this->Recipes->find('all', array(
$filter = [];
foreach ($ingredients["data"] as $ingredientId) {
array_push($filter, ['IngredientMappings.ingredient_id' => $ingredientId]);
}
$query = $this->Recipes->find('all', [
'recursive' => 0,
'fields' => array(
'fields' => [
'id',
'name',
'COUNT(*) as matches'),
'group' => array('Recipe.id', 'Recipe.name'),
'joins' => array(
array(
'alias' => 'IngredientMapping',
'table' => 'ingredient_mappings',
'foreignKey' => false,
'conditions' => array('IngredientMapping.recipe_id = Recipe.id'),
),
),
'conditions' => array(
'IngredientMapping.ingredient_id'=> $ingredients
),
'limit' => 20,
'order' => array('matches DESC')
));
$this->set('recipes', $results);
'matches' => "count(*)"]
]
)->innerJoinWith('IngredientMappings')
->where(['OR' => $filter])
->group(['Recipes.id', 'Recipes.name'])
->order("matches DESC")
->limit(20);

$recipes = $query->toArray();

$this->set(compact('recipes'));
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/Template/Recipes/contains.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ $baseUrl = Router::url('/');
<?php foreach ($recipes as $recipe): ?>
<tr>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $recipe['Recipe']['id']), array('class' => 'ajaxNavigation')); ?>
<?php if ($loggedIn && ($isAdmin || $loggedInuserId == $recipe['User']['id'])):?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $recipe['Recipe']['id']), array('class' => 'ajaxNavigation')); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $recipe['Recipe']['id']), null, __('Are you sure you want to delete %s?', $recipe['Recipe']['name'])); ?>
<?php if (isset($recipe->private) && $recipe->private == 'true' && $loggedInuserId != $recipe->user->id && !$isEditor) {
echo __('(private)');
} else {
echo $this->Html->link(__('View'), array('action' => 'view', $recipe->id), array('class' => 'ajaxNavigation'));
}
if ($loggedIn && ($isAdmin || $loggedInuserId == $recipe->user->id)):?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $recipe->id), array('class' => 'ajaxNavigation')); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $recipe->id), ['confirm' => __('Are you sure you want to delete {0}?', $recipe->name)]); ?>
<?php endif;?>
</td>
<td><?php echo h($recipe['Recipe']['name']); ?>&nbsp;</td>
<td><?php echo $recipe[0]['matches'];?></td>
<td><?php echo h($recipe->name); ?>&nbsp;</td>
<td><?php echo $recipe->matches;?></td>
</tr>
<?php endforeach; endif; ?>

0 comments on commit 032df4f

Please sign in to comment.