1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleCrud\Queries; |
4
|
|
|
|
5
|
|
|
use SimpleCrud\RowInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Extended trait |
9
|
|
|
* |
10
|
|
|
* @property \SimpleCrud\Entity $entity |
11
|
|
|
*/ |
12
|
|
|
trait ExtendedSelectionTrait |
13
|
|
|
{ |
14
|
|
|
use SelectionTrait; |
15
|
|
|
|
16
|
|
|
protected $from = []; |
17
|
|
|
protected $fields = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Adds new extra table to the query. |
21
|
|
|
* |
22
|
|
|
* @param string $table |
23
|
|
|
* |
24
|
|
|
* @return self |
25
|
|
|
*/ |
26
|
|
|
public function from($table) |
27
|
|
|
{ |
28
|
|
|
$this->from[] = $table; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Adds a new extra field to the query. |
35
|
|
|
* |
36
|
|
|
* @param string $field |
37
|
|
|
* |
38
|
|
|
* @return self |
39
|
|
|
*/ |
40
|
|
|
public function field($field) |
41
|
|
|
{ |
42
|
|
|
$this->fields[] = $field; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Adds a WHERE according with the relation of other entity. |
49
|
|
|
* |
50
|
|
|
* @param RowInterface $row |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
|
|
public function relatedWith(RowInterface $row) |
55
|
|
|
{ |
56
|
|
|
$entity = $row->getEntity(); |
57
|
|
|
|
58
|
|
|
if ($this->entity->hasOne($entity)) { |
59
|
|
|
return $this->by($entity->foreignKey, $row->get('id')); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->entity->hasMany($entity)) { |
63
|
|
|
return $this->byId($row->get($this->entity->foreignKey)); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$bridge = $this->entity->getBridge($entity); |
67
|
|
|
|
68
|
|
|
if ($bridge) { |
69
|
|
|
$this->from($bridge->name); |
70
|
|
|
$this->from($entity->name); |
71
|
|
|
|
72
|
|
|
$this->fields[] = "`{$bridge->name}`.`{$entity->foreignKey}`"; |
73
|
|
|
|
74
|
|
|
$this->where("`{$bridge->name}`.`{$this->entity->foreignKey}` = `{$this->entity->name}`.`id`"); |
75
|
|
|
$this->where("`{$bridge->name}`.`{$entity->foreignKey}` = `{$entity->name}`.`id`"); |
76
|
|
|
$this->where("`{$entity->name}`.`id` IN (:{$bridge->name})", [":{$bridge->name}" => $row->get('id')]); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new SimpleCrudException("The tables {$this->entity->name} and {$entity->name} are no related"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* add extra fields to the code. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
protected function fieldsToString($prepend = ', ') |
90
|
|
|
{ |
91
|
|
|
return $this->fields ? $prepend.implode(', ', $this->fields) : ''; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* add extra fields to the code. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function fromToString($prepend = ', ') |
100
|
|
|
{ |
101
|
|
|
return $this->from ? $prepend.'`'.implode('`, `', $this->from).'`' : ''; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: