-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableRelation.php
717 lines (553 loc) · 20.5 KB
/
TableRelation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/orm/blob/master/LICENSE
*/
namespace Quid\Orm;
use Quid\Base;
// tableRelation
// class to access the relation data of a table
class TableRelation extends Relation
{
// config
protected static array $config = [];
// construct
// construit l'objet de relation de table
final public function __construct(Table $table)
{
$this->setLink($table,false);
$this->makeAttr($table);
}
// makeAttr
// applique les attributs de relation en provenance de la table
// si what est true, prend le nom de la colonne via la méthode colName
// seul what est nécessaire
final protected function makeAttr($table,bool $config=true):void
{
if(!$table->allowsRelation())
static::throw($this,'doesNotSupportRelation');
$attr = $table->getAttr('relation');
if(is_scalar($attr))
$attr = ['what'=>$attr];
if(!is_array($attr))
$attr = [];
if(!array_key_exists('what',$attr))
$attr['what'] = true;
if($attr['what'] === true)
{
$colName = $table->colName();
$value = [];
if(!empty($colName))
$value[] = $colName->name();
$attr['what'] = $value;
}
if(!is_array($attr['what']))
$attr['what'] = [$attr['what']];
if(empty($attr['what']))
static::throw('whatCannotBeEmpty');
if(array_key_exists('method',$attr) && is_string($attr['method']))
$attr = $this->prepareAttrWithMethod($table,$attr);
else
$attr = $this->prepareAttrWithWhat($table,$attr);
foreach ($attr as $key => $value)
{
if(!empty($value) && static::isCallable($value))
{
$value = $value($this);
$attr[$key] = Base\Obj::cast($value);
}
if($key === 'where' && !is_array($attr[$key]))
$attr[$key] = (array) $attr[$key];
}
$this->attr = $attr;
}
// prepareAttrWithWhat
// prépare les attributs pour une relation de table standard avec what
final protected function prepareAttrWithWhat(Table $table,array $attr):array
{
$primary = $table->primary();
$attr['output'] ??= null;
if($attr['output'] === null)
$attr['output'] = $attr['what'];
if(!is_array($attr['output']))
$attr['output'] = [$attr['output']];
$attr['where'] ??= null;
$attr['order'] ??= $table->order();
$attr['onGet'] ??= false;
return $attr;
}
// prepareAttrWithMethod
// prépare les attributs pour une relation de table avec output de méthode
final protected function prepareAttrWithMethod(Table $table,array $return):array
{
$primary = $table->primary();
if(!array_key_exists('method',$return) || !is_string($return['method']))
static::throw('methodMustBeString');
$return['where'] ??= null;
$return['order'] ??= [$primary=>'asc'];
return $return;
}
// searchMinLength
// retourne la longueur minimale de la recherche
final public function searchMinLength():int
{
return $this->table()->searchMinLength();
}
// shouldCache
// retourne si l'argument cache doit être respecté
final public function shouldCache(bool $return,$option=null)
{
$option = (array) $option;
$option = Base\Arr::clean($option);
return (!empty($option))? false:$return;
}
// isOutputMethod
// retourne vrai si la output est une méthode de row
final public function isOutputMethod(?string $method=null):bool
{
if($method === null)
{
$attr = $this->attr();
$method ??= $attr['method'] ?? null;
}
return is_string($method);
}
// size
// compte le nombre de relation
final public function size(bool $cache=true,?array $option=null):int
{
$cache = $this->shouldCache($cache,$option);
$option = Base\Arr::plus($this->attr(),$option);
$where = $option['where'] ?? null;
return $this->table()->rowsCount(true,$cache,$where);
}
// get
// retourne une relation dans la table
// une clé primaire doit être fourni
// par défaut les relations sont conservés en cache dans l'objet relation
final public function get(int $primary,bool $cache=true,?array $option=null)
{
$return = null;
$relations = $this->gets([$primary],false,$cache,$option);
if(!empty($relations))
$return = current($relations);
return $return;
}
// gets
// retourne plusieurs relations dans la table
// seul les relations non chargés le sont
// par défaut les relations sont conservés en cache dans l'objet relation
final public function gets(array $primaries,bool $found=false,bool $cache=true,?array $option=null):array
{
$return = [];
$attr = $this->attr();
$cache = $this->shouldCache($cache,$option);
$option = Base\Arr::plus($attr,$option);
$what = $option['what'];
$where = $option['where'] ?? null;
$method = (isset($option['method']) && is_string($option['method']))? $option['method']:null;
$data =& $this->arr();
$isMethod = $this->isOutputMethod($method);
if($cache === true && !empty($data))
$return = Base\Arr::getsExists($primaries,$data);
if(count($return) !== count($primaries))
{
$missing = (!empty($return))? Base\Arr::valuesStrip(array_keys($return),$primaries):$primaries;
if(empty($missing))
static::throw();
if(!empty($return) && $found === false)
$return = Base\Arr::gets($primaries,$data);
$db = $this->db();
$table = $this->table();
$primary = $table->primary();
$where[] = [$primary,'in',$missing];
if($isMethod === true)
$result = $db->rows($table,$where);
else
{
$what = Base\Arr::merge($primary,$what);
$result = $db->selectAssocsUnique($what,$table,$where);
}
if(is_array($result) || $result instanceof Rows)
{
foreach ($result as $key => $value)
{
$return[$key] = $this->makeOutput($value,$option);
if($cache === true)
$data[$key] = $return[$key];
}
$return = Base\Arr::getsExists($primaries,$return);
}
}
return $return;
}
// all
// retourne un tableau avec toutes les relations de la table
// par défaut les relations sont conservés en cache dans l'objet relation
// il y a un problème si tu charges les relations via relation ou relations et ensuite relationAll avec la cache (l'ordre ne sera pas respecté)
// retourne une référence
final public function &all(bool $cache=true,?array $option=null):array
{
$data =& $this->arr();
$cache = $this->shouldCache($cache,$option);
$attr = $this->attr();
$option = Base\Arr::plus($attr,$option);
$where = $option['where'] ?? [];
$order = $this->getOrder($option['order'],$option);
$limit = $option['limit'] ?? null;
$not = (isset($option['not']) && is_array($option['not']))? $option['not']:null;
$method = (isset($option['method']) && is_string($option['method']))? $option['method']:null;
$isMethod = $this->isOutputMethod($method);
if(!($cache === true && count($data) === $this->size($cache)))
{
$attr = $this->attr();
if(!empty($attr))
{
$new = [];
$db = $this->db();
$table = $this->table();
$primary = $table->primary();
if(!empty($not))
$where[] = [$primary,'notIn',$not];
if($cache === true && !empty($data))
$where[] = [$primary,'notIn',array_keys($data)];
if($isMethod === true)
$result = $db->rows($table,$where,$order,$limit);
else
{
$what = Base\Arr::merge($primary,$attr['what']);
$result = $db->selectAssocsUnique($what,$table,$where,$order,$limit);
}
if(is_array($result) || $result instanceof Rows)
{
$new = [];
if($cache === true && !empty($data))
$new = $data;
foreach ($result as $key => $value)
{
$new[$key] = $this->makeOutput($value,$option);
if($cache === true)
$data[$key] = $new[$key];
}
if($cache === false)
$data = $new;
}
}
}
return $data;
}
// exists
// retourne vrai si la ou les clés existent dans la relation
final public function exists(...$primaries):bool
{
return $this->existsWhere(null,...$primaries);
}
// existsWhere
// retourne vrai si la ou les clés existent dans la relation
// prend compte de where
// cache est true par défaut
final public function existsWhere($where=null,...$primaries):bool
{
$return = false;
if(!empty($primaries))
{
$cache = $this->shouldCache(true,$where);
$data = null;
if($where === null)
$data = $this->arr();
if(!empty($data) && Base\Arr::keysExists($primaries,$data))
$return = true;
elseif($this->size() > 0)
{
$db = $this->db();
$attr = $this->attr();
$table = $this->table();
$primary = $table->primary();
$where = Base\Arr::plus($attr['where'],$where);
$where[] = [$primary,'in',$primaries];
$count = count($primaries);
$return = ($db->selectCount($table,$where) === $count);
}
}
return $return;
}
// in
// retourne vrai si la ou les valeurs existent dans la relation
final public function in(...$values):bool
{
return $this->inWhere(null,...$values);
}
// inWhere
// retourne vrai si la ou les valeurs existent dans la relation
// prend compte de where
// cette méthode utilisera all si pas trouvé dans les relations existantes, donc la cache est true par défaut
final public function inWhere($where=null,...$values):bool
{
$return = false;
if(!empty($values))
{
$cache = $this->shouldCache(true,$where);
$data = null;
if($where === null)
$data = $this->arr();
if(!empty($data) && Base\Arr::ins($values,$data))
$return = true;
elseif($this->size() > 0)
{
$all = $this->all(true);
$return = (!empty($all) && Base\Arr::ins($values,$all));
}
}
return $return;
}
// search
// permet de faire une recherche dans la relation
// la méthode renvoie à la méthode search dans core/table, donc les mêmes règles s'appliquent (minimum searchTerm, support pour +)
// n'utilise pas la cache de relation
final public function search(string $value,?array $option=null):?array
{
$return = null;
$result = $this->searchResult($value,$option);
if($result !== null)
{
$return = [];
foreach ($result as $key => $value)
{
$return[$key] = $this->makeOutput($value,$option);
}
}
return $return;
}
// searchCount
// retourne le nombre de résultat d'une recherche dans la relation
// n'utilise pas la cache de relation
final public function searchCount(string $value,?array $option=null):?int
{
$return = null;
$option = Base\Arr::plus($option,['limit'=>null,'method'=>null]);
$result = $this->searchResult($value,$option);
if($result !== null)
$return = count($result);
return $return;
}
// searchResult
// utilisé par search et searchCount
final protected function searchResult(string $value,?array $option=null)
{
$return = null;
$attr = $this->attr();
$option = Base\Arr::plus($attr,$option);
$table = $this->table();
if(strlen($value) && $this->size() > 0)
{
$return = [];
$sqlArray =[];
$primary = $table->primary();
$cols = $option['what'] ?? $table->cols()->searchable();
$method = (isset($option['method']) && is_string($option['method']))? $option['method']:null;
$isMethod = $this->isOutputMethod($method);
$what = ($isMethod === true)? '*':Base\Arr::merge($primary,$cols);
$output = ($isMethod === true)? 'rows':'assocsUnique';
$where = $option['where'] ?? [];
$whereNot = (isset($option['not']) && is_array($option['not']))? $option['not']:null;
if(!empty($whereNot))
$where[] = [$primary,'notIn',$whereNot];
$sqlArray['what'] = $what;
$sqlArray['where'] = $where;
$sqlArray['order'] = $this->getOrder($option['order'],$option);
$sqlArray['limit'] = $option['limit'] ?? null;
$sqlArray['search'] = $value;
$sqlArray['searchCols'] = $cols;
$sqlArray['searchSeparator'] = $option['searchSeparator'] ?? null;
$sqlArray['searchMethod'] = $option['searchMethod'] ?? null;
$sqlArray['searchTermValid'] = $option['searchTermValid'] ?? true;
$sql = $table->sql($sqlArray);
$result = $sql->trigger($output);
if(is_array($result) || $result instanceof Rows)
$return = $result;
}
return $return;
}
// defaultOrderCode
// retourne le code d'ordre par défaut pour la relation
final public function defaultOrderCode():?int
{
return $this->table()->getAttr('orderCode');
}
// getOrder
// génère le order pour la relation de table
// support pour order 1, 2, 3 et 4
final public function getOrder($order=null,?array $attr=null):?array
{
$return = null;
$attr ??= $this->attr();
$attrOrder = $attr['order'] ?? null;
$table = $this->table();
if(is_array($order))
$return = $order;
elseif(is_int($order))
{
$table = $this->table();
$primary = $table->primary();
$allowed = $this->allowedOrdering($attr);
$field = $this->getOrderFieldOutput($attr);
if($order === 1 && !empty($allowed['key']))
$return = [$primary=>'asc'];
elseif($order === 2 && !empty($allowed['key']))
$return = [$primary=>'desc'];
elseif($order === 3 && is_string($field) && !empty($allowed['value']))
$return = [$field=>'asc'];
elseif($order === 4 && is_string($field) && !empty($allowed['value']))
$return = [$field=>'desc'];
}
elseif(is_array($attrOrder))
$return = $attrOrder;
elseif($attrOrder === null)
$return = $table->order();
return $return;
}
// allowedOrdering
// retourne un tableau définissant si la relation peut être ordonner par clé et ou valeur
final public function allowedOrdering(?array $attr=null):array
{
$return = ['key'=>true];
if(is_string($this->getOrderFieldOutput($attr)))
$return['value'] = true;
return $return;
}
// getOrderFieldOutput
// retourne le champ à utiliser pour l'ordonnage de nom
// envoie une exception si aucun champ trouvé
final public function getOrderFieldOutput(?array $attr=null):?string
{
$return = $this->table()->colName()->name();
$attr ??= $this->attr();
if($this->isOutputMethod() === false)
{
$output = $attr['output'] ?? null;
$field = null;
if(!empty($output))
{
if(is_array($output))
$field = current($output);
elseif(is_string($output))
$field = $output;
if(is_string($field) && !empty($field))
{
if(strpos($field,'[') !== false && strpos($field,'_[') === false)
{
$segment = Base\Segment::get(null,$field);
if(is_array($segment) && !empty($segment))
$return = current($segment);
}
else
$return = $field;
}
}
}
return $return;
}
// makeOutput
// méthode privé pour généré un output via output ou outputMethod
final protected function makeOutput($value,?array $option=null):?string
{
$return = null;
$attr = $this->attr();
$method = $attr['method'] ?? null;
$option = Base\Arr::plus($attr,$option);
$method = (isset($option['method']) && is_string($option['method']))? $option['method']:$method;
$output = $option['output'] ?? null;
$onGet = $option['onGet'] ?? false;
$isMethod = $this->isOutputMethod($method);
if($isMethod === true)
$return = $this->outputMethod($value,$method);
else
$return = $this->output($value,$output,$onGet);
return $return;
}
// output
// gère le output d'un tableau associatif contenant les données sur la relation
// possible de passer le tableau dans onGet, par défaut c'est faux
// output peut être une string, un array ou null
final public function output(array $array,$output,bool $onGet=false):?string
{
$return = null;
if($onGet === true)
{
$cols = $this->table()->cols();
$array = $cols->value($array,true,true);
}
if($output === true)
{
$attr = $this->attr();
$output = $attr['output'] ?? null;
}
if(is_array($output))
{
$r = '';
foreach ($output as $out)
{
$v = null;
if($out === null)
$v = $array;
elseif(is_string($out) && strpos($out,'[') !== false)
$v = Base\Segment::sets(null,$array,$out);
elseif(is_string($out))
$v = Base\Arr::get($out,$array);
if(!empty($v))
{
if(is_array($v))
{
foreach ($v as $kk => $vv)
{
$r = $this->outputAdd($r,$kk,$vv);
}
}
else
$r = $this->outputAdd($r,$out,$v);
}
}
$return = $r;
}
if(is_scalar($return))
$return = (string) $return;
if(empty($return) || !is_string($return))
$return = null;
return $return;
}
// outputAdd
// utilisé par la méthode output pour ajouter un élément à la string de sortie
final protected function outputAdd(string $return,$key,$value,string $separator=' - '):string
{
if(is_scalar($value))
{
$table = $this->table();
$primary = $table->primary();
$value = (string) $value;
if(strlen($value))
{
if($key === $primary && strlen($return))
$return = static::appendPrimary($return,$value);
else
{
$return .= (strlen($return))? $separator:'';
$return .= $value;
}
}
}
return $return;
}
// outputMethod
// gère le output d'une relation avec output method
final public function outputMethod(Row $row,string $method):?string
{
$return = $row->$method();
$return = Base\Obj::cast($return);
if(is_scalar($return))
$return = (string) $return;
return $return;
}
}
?>