|
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
|
|
|
|
|
9
|
|
|
Trait ManyToManyRelation |
|
10
|
|
|
{ |
|
11
|
|
|
// These variables are relevant for internal bookkeeping (constraint generation etc) |
|
12
|
|
|
/** @var string The name of the left column of the relation. */ |
|
13
|
|
|
private $_leftColumnName; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string The name of the right column of the relation. */ |
|
16
|
|
|
private $_rightColumnName; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string The name of the left table of the relation. */ |
|
19
|
|
|
private $_leftEntityTable; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string The name of the right table of the relation. */ |
|
22
|
|
|
private $_rightEntityTable; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initializes the the ManyToManyRelation trait on the included object |
|
26
|
|
|
* |
|
27
|
|
|
* @param AbstractActiveRecord $leftEntity The left entity of the relation |
|
28
|
|
|
* @param &variable $leftVariable The variable where the id for the left entity will be stored |
|
|
|
|
|
|
29
|
|
|
* @param AbstractActiveRecord $rightEntity The left entity of the relation |
|
30
|
|
|
* @param &variable $leftVariable The variable where the id for the right entity will be stored |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
3 |
|
protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable) |
|
33
|
|
|
{ |
|
34
|
3 |
|
$this->_leftEntityTable = $leftEntity->getActiveRecordTable(); |
|
|
|
|
|
|
35
|
3 |
|
$this->_rightEntityTable = $rightEntity->getActiveRecordTable(); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
3 |
|
if (get_class($leftEntity) === get_class($rightEntity)) { |
|
38
|
3 |
|
$this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getActiveRecordTable()); |
|
|
|
|
|
|
39
|
3 |
|
$this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getActiveRecordTable()); |
|
|
|
|
|
|
40
|
|
|
} else { |
|
41
|
|
|
$this->_leftColumnName = sprintf("id_%s", $leftEntity->getActiveRecordTable()); |
|
|
|
|
|
|
42
|
|
|
$this->_rightColumnName = sprintf("id_%s", $rightEntity->getActiveRecordTable()); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
$this->extendTableDefinition($this->_leftColumnName, [ |
|
|
|
|
|
|
46
|
3 |
|
'value' => &$leftVariable, |
|
47
|
|
|
'validate' => null, |
|
48
|
|
|
'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
49
|
3 |
|
'properties' => ColumnProperty::NOT_NULL |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
3 |
|
$this->extendTableDefinition($this->_rightColumnName, [ |
|
|
|
|
|
|
53
|
3 |
|
'value' => &$rightVariable, |
|
54
|
|
|
'validate' => null, |
|
55
|
|
|
'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
56
|
3 |
|
'properties' => ColumnProperty::NOT_NULL |
|
57
|
|
|
]); |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Build the constraints for the many-to-many relation table |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function createTableConstraints() |
|
64
|
|
|
{ |
|
65
|
3 |
|
$childTable = $this->getActiveRecordTable(); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
3 |
|
$leftParentTable = $this->_leftEntityTable; |
|
68
|
3 |
|
$rightParentTable = $this->_rightEntityTable; |
|
69
|
|
|
|
|
70
|
3 |
|
$leftConstraint = $this->buildConstraint($leftParentTable, 'id', $childTable, $this->_leftColumnName); |
|
|
|
|
|
|
71
|
3 |
|
$rightConstraint = $this->buildConstraint($rightParentTable, 'id', $childTable, $this->_rightColumnName); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
3 |
|
$this->pdo->query($leftConstraint); |
|
|
|
|
|
|
74
|
3 |
|
$this->pdo->query($rightConstraint); |
|
75
|
3 |
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.