@@ -72,20 +72,24 @@ public function deleteRecipeById(int $id) {
72
72
73
73
public function findAllRecipes (string $ user_id ) {
74
74
$ qb = $ this ->db ->getQueryBuilder ();
75
-
76
- $ qb ->select (' * ' )
75
+
76
+ $ qb ->select ([ ' r.recipe_id ' , ' r.name ' , ' k.name AS keywords ' ] )
77
77
->from (self ::DB_TABLE_RECIPES , 'r ' )
78
- ->where ('user_id = :user ' )
78
+ ->where ('r. user_id = :user ' )
79
79
->orderBy ('r.name ' );
80
80
$ qb ->setParameter ('user ' , $ user_id , TYPE ::STRING );
81
+ $ qb ->leftJoin ('r ' , self ::DB_TABLE_KEYWORDS , 'k ' , 'r.recipe_id = k.recipe_id ' );
81
82
82
83
$ cursor = $ qb ->execute ();
83
84
$ result = $ cursor ->fetchAll ();
84
85
$ cursor ->closeCursor ();
85
86
86
87
$ result = $ this ->sortRecipes ($ result );
87
-
88
- return $ this ->unique ($ result );
88
+
89
+ // group recipes, convert keywords to comma-separated list
90
+ $ recipesGroupedTags = $ this ->groupKeywordInResult ($ result );
91
+
92
+ return $ this ->unique ($ recipesGroupedTags );
89
93
}
90
94
91
95
public function unique (array $ result ) {
@@ -196,23 +200,28 @@ public function getRecipesByCategory(string $category, string $user_id) {
196
200
197
201
if ($ category != '_ ' )
198
202
{
199
- $ qb ->select (['r.recipe_id ' , 'r.name ' ])
200
- ->from (self ::DB_TABLE_CATEGORIES , 'k ' )
201
- ->where ('k.name = :category ' )
202
- ->andWhere ('k.user_id = :user ' )
203
+ // One would probably want to use GROUP_CONCAT to create the list of keywords
204
+ // for the recipe, but those don't seem to work:
205
+ // $qb->select(['r.recipe_id', 'r.name', 'GROUP_CONCAT(k.name) AS keywords']) // not working
206
+ // $qb->select(['r.recipe_id', 'r.name', DB::raw('GROUP_CONCAT(k.name) AS keywords')]) // not working
207
+ $ qb ->select (['r.recipe_id ' , 'r.name ' , 'k.name AS keywords ' ])
208
+ ->from (self ::DB_TABLE_CATEGORIES , 'c ' )
209
+ ->where ('c.name = :category ' )
210
+ ->andWhere ('c.user_id = :user ' )
203
211
->setParameter ('category ' , $ category , TYPE ::STRING )
204
212
->setParameter ('user ' , $ user_id , TYPE ::STRING );
205
213
206
- $ qb ->join ('k ' , self ::DB_TABLE_RECIPES , 'r ' , 'k.recipe_id = r.recipe_id ' );
214
+ $ qb ->join ('c ' , self ::DB_TABLE_RECIPES , 'r ' , 'c.recipe_id = r.recipe_id ' );
215
+ $ qb ->leftJoin ('c ' , self ::DB_TABLE_KEYWORDS , 'k ' , 'c.recipe_id = k.recipe_id ' );
207
216
208
- $ qb ->groupBy (['r.name ' , 'r.recipe_id ' ]);
209
- $ qb ->orderBy ('r.name ' );
217
+ $ qb ->groupBy (['r.name ' , 'r.recipe_id ' , ' k.name ' ]);
218
+ $ qb ->orderBy ('r.name ' );
210
219
}
211
220
else
212
221
{
213
-
214
- $ qb ->select (['r.recipe_id ' , 'r.name ' ])
222
+ $ qb ->select (['r.recipe_id ' , 'r.name ' , 'k.name AS keywords ' ])
215
223
->from (self ::DB_TABLE_RECIPES , 'r ' )
224
+ ->leftJoin ('r ' , self ::DB_TABLE_KEYWORDS , 'k ' , 'r.recipe_id = k.recipe_id ' )
216
225
->leftJoin (
217
226
'r ' ,
218
227
self ::DB_TABLE_CATEGORIES ,
@@ -231,8 +240,11 @@ public function getRecipesByCategory(string $category, string $user_id) {
231
240
$ cursor = $ qb ->execute ();
232
241
$ result = $ cursor ->fetchAll ();
233
242
$ cursor ->closeCursor ();
243
+
244
+ // group recipes, convert keywords to comma-separated list
245
+ $ recipesGroupedTags = $ this ->groupKeywordInResult ($ result );
234
246
235
- return $ this ->unique ($ result );
247
+ return $ this ->unique ($ recipesGroupedTags );
236
248
}
237
249
238
250
/**
@@ -267,13 +279,14 @@ public function getRecipesByKeywords(string $keywords, string $user_id) {
267
279
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
268
280
*/
269
281
public function findRecipes (array $ keywords , string $ user_id ) {
282
+
270
283
$ has_keywords = $ keywords && is_array ($ keywords ) && sizeof ($ keywords ) > 0 && $ keywords [0 ];
271
284
272
285
if (!$ has_keywords ) { return $ this ->findAllRecipes ($ user_id ); }
273
286
274
287
$ qb = $ this ->db ->getQueryBuilder ();
275
288
276
- $ qb ->select (['r.recipe_id ' , 'r.name ' ])
289
+ $ qb ->select (['r.recipe_id ' , 'r.name ' , ' k.name AS keywords ' ])
277
290
->from (self ::DB_TABLE_RECIPES , 'r ' );
278
291
279
292
$ qb ->leftJoin ('r ' , self ::DB_TABLE_KEYWORDS , 'k ' , 'k.recipe_id = r.recipe_id ' );
@@ -309,7 +322,28 @@ public function findRecipes(array $keywords, string $user_id) {
309
322
$ result = $ cursor ->fetchAll ();
310
323
$ cursor ->closeCursor ();
311
324
312
- return $ this ->unique ($ result );
325
+ // group recipes, convert keywords to comma-separated list
326
+ $ recipesGroupedTags = $ this ->groupKeywordInResult ($ result );
327
+
328
+ return $ this ->unique ($ recipesGroupedTags );
329
+ }
330
+
331
+ /**
332
+ * @param array $results Array of recipes with double entries for different keywords
333
+ * Group recipes by id and convert keywords to comma-separated list
334
+ */
335
+ public function groupKeywordInResult (array $ result ) {
336
+ $ recipesGroupedTags = array ();
337
+ foreach ($ result as $ recipe ) {
338
+ if (!array_key_exists ($ recipe ['recipe_id ' ], $ recipesGroupedTags )){
339
+ $ recipesGroupedTags [$ recipe ['recipe_id ' ]] = $ recipe ;
340
+ } else {
341
+ if (!is_null ($ recipe ['keywords ' ])) {
342
+ $ recipesGroupedTags [$ recipe ['recipe_id ' ]]['keywords ' ] .= ', ' .$ recipe ['keywords ' ];
343
+ }
344
+ }
345
+ }
346
+ return $ recipesGroupedTags ;
313
347
}
314
348
315
349
/**
0 commit comments