1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace miBadger\ActiveRecord\Traits; |
4
|
|
|
|
5
|
|
|
use miBadger\Query\Query; |
6
|
|
|
use miBadger\ActiveRecord\ColumnProperty; |
7
|
|
|
use miBadger\ActiveRecord\AbstractActiveRecord; |
8
|
|
|
use miBadger\ActiveRecord\SchemaBuilder; |
9
|
|
|
|
10
|
|
|
Trait ManyToManyRelation |
11
|
|
|
{ |
12
|
|
|
// These variables are relevant for internal bookkeeping (constraint generation etc) |
13
|
|
|
|
14
|
|
|
/** @var string The name of the left column of the relation. */ |
15
|
|
|
private $_leftColumnName; |
16
|
|
|
|
17
|
|
|
/** @var string The name of the right column of the relation. */ |
18
|
|
|
private $_rightColumnName; |
19
|
|
|
|
20
|
|
|
/** @var string The name of the left table of the relation. */ |
21
|
|
|
private $_leftEntityTable; |
22
|
|
|
|
23
|
|
|
/** @var string The name of the right table of the relation. */ |
24
|
|
|
private $_rightEntityTable; |
25
|
|
|
|
26
|
|
|
/** @var \PDO The PDO object. */ |
27
|
|
|
protected $pdo; |
28
|
|
|
/** |
29
|
|
|
* Initializes the the ManyToManyRelation trait on the included object |
30
|
|
|
* |
31
|
|
|
* @param AbstractActiveRecord $leftEntity The left entity of the relation |
32
|
|
|
* @param int $leftVariable The reference to the variable where the id for the left entity will be stored |
33
|
|
|
* @param AbstractActiveRecord $rightEntity The left entity of the relation |
34
|
|
|
* @param int $leftVariable The reference to the variable where the id for the right entity will be stored |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
3 |
|
protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable) |
38
|
|
|
{ |
39
|
3 |
|
$this->_leftEntityTable = $leftEntity->getTableName(); |
40
|
3 |
|
$this->_rightEntityTable = $rightEntity->getTableName(); |
41
|
|
|
|
42
|
3 |
|
if (get_class($leftEntity) === get_class($rightEntity)) { |
43
|
3 |
|
$this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getTableName()); |
44
|
3 |
|
$this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getTableName()); |
45
|
|
|
} else { |
46
|
|
|
$this->_leftColumnName = sprintf("id_%s", $leftEntity->getTableName()); |
47
|
|
|
$this->_rightColumnName = sprintf("id_%s", $rightEntity->getTableName()); |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
$this->extendTableDefinition($this->_leftColumnName, [ |
51
|
3 |
|
'value' => &$leftVariable, |
52
|
|
|
'validate' => null, |
53
|
|
|
'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
54
|
3 |
|
'properties' => ColumnProperty::NOT_NULL |
55
|
|
|
]); |
56
|
|
|
|
57
|
3 |
|
$this->extendTableDefinition($this->_rightColumnName, [ |
58
|
3 |
|
'value' => &$rightVariable, |
59
|
|
|
'validate' => null, |
60
|
|
|
'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
61
|
3 |
|
'properties' => ColumnProperty::NOT_NULL |
62
|
|
|
]); |
63
|
3 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Build the constraints for the many-to-many relation table |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
3 |
|
public function createTableConstraints() |
70
|
|
|
{ |
71
|
3 |
|
$childTable = $this->getTableName(); |
|
|
|
|
72
|
|
|
|
73
|
3 |
|
$leftParentTable = $this->_leftEntityTable; |
74
|
3 |
|
$rightParentTable = $this->_rightEntityTable; |
75
|
|
|
|
76
|
3 |
|
$leftConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($leftParentTable, 'id', $childTable, $this->_leftColumnName); |
|
|
|
|
77
|
3 |
|
$rightConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($rightParentTable, 'id', $childTable, $this->_rightColumnName); |
78
|
|
|
|
79
|
3 |
|
$this->pdo->query($leftConstraint); |
80
|
3 |
|
$this->pdo->query($rightConstraint); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
abstract public function getTableName(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
abstract protected function extendTableDefinition(string $columnName, $definition); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
abstract protected function registerSearchHook(string $columnName, $fn); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
abstract protected function registerDeleteHook(string $columnName, $fn); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
abstract protected function registerUpdateHook(string $columnName, $fn); |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
abstract protected function registerReadHook(string $columnName, $fn); |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
abstract protected function registerCreateHook(string $columnName, $fn); |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.