|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\model; |
|
3
|
|
|
|
|
4
|
|
|
use Propel\Generator\Model\CrossForeignKeys; |
|
5
|
|
|
use Propel\Generator\Model\ForeignKey; |
|
6
|
|
|
use Propel\Generator\Model\Table; |
|
7
|
|
|
use phootwork\collection\Set; |
|
8
|
|
|
use phootwork\collection\Map; |
|
9
|
|
|
|
|
10
|
|
|
class ManyToManyRelationship extends Relationship { |
|
11
|
|
|
|
|
12
|
|
|
/** @var ForeignKey */ |
|
13
|
|
|
protected $lk; |
|
14
|
|
|
|
|
15
|
|
|
/** @var CrossForeignKeys */ |
|
16
|
|
|
protected $cfk; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Table $model, CrossForeignKeys $cfk) { |
|
19
|
|
|
$this->model = $model; |
|
20
|
|
|
$this->cfk = $cfk; |
|
21
|
|
|
|
|
22
|
|
|
$fkTables = new Map(); |
|
23
|
|
|
foreach ($cfk->getMiddleTable()->getForeignKeys() as $fk) { |
|
24
|
|
|
if (!$fkTables->has($fk->getForeignTableCommonName())) { |
|
25
|
|
|
$fkTables->set($fk->getForeignTableCommonName(), 0); |
|
26
|
|
|
} |
|
27
|
|
|
$fkTables->set($fk->getForeignTableCommonName(), $fkTables->get($fk->getForeignTableCommonName()) + 1); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$idColumns = []; |
|
31
|
|
|
if ($fkTables->get($model->getCommonName()) > 1) { |
|
32
|
|
|
$name = ''; |
|
33
|
|
|
$splits = explode('_', $cfk->getMiddleTable()->getOriginCommonName()); |
|
34
|
|
|
foreach ($splits as $split) { |
|
35
|
|
|
if (empty($name)) { |
|
36
|
|
|
$name = $split; |
|
37
|
|
|
} else { |
|
38
|
|
|
$name .= '_' . $split; |
|
39
|
|
|
$idColumns []= $split . '_id'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$idColumns []= $name . '_id'; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
foreach ($cfk->getMiddleTable()->getForeignKeys() as $fk) { |
|
47
|
|
|
// looks like a many-to-many parent + child relationship |
|
48
|
|
|
if ($fkTables->get($model->getCommonName()) > 1) { |
|
49
|
|
|
if (in_array($fk->getLocalColumnName(), $idColumns)) { |
|
50
|
|
|
$this->lk = $fk; |
|
51
|
|
|
} else { |
|
52
|
|
|
$this->fk = $fk; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// normal many-to-many relationship |
|
57
|
|
|
else { |
|
58
|
|
|
if ($fk->getForeignTable() != $model) { |
|
59
|
|
|
$this->fk = $fk; |
|
60
|
|
|
} else if ($fk->getForeignTable() == $model) { |
|
61
|
|
|
$this->lk = $fk; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($this->fk === null) { |
|
67
|
|
|
echo $cfk->getMiddleTable()->getOriginCommonName() . "\n"; |
|
68
|
|
|
echo $cfk->getTable()->getOriginCommonName() . "\n"; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->foreign = $this->fk->getForeignTable(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the type of this relationship |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getType() { |
|
80
|
|
|
return self::MANY_TO_MANY; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the middle table |
|
85
|
|
|
* |
|
86
|
|
|
* @return Table |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getMiddle() { |
|
89
|
|
|
return $this->cfk->getMiddleTable(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the cross foreign keys |
|
94
|
|
|
* |
|
95
|
|
|
* @return CrossForeignKeys |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getCrossForeignKeys() { |
|
98
|
|
|
return $this->cfk; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the local key |
|
103
|
|
|
* |
|
104
|
|
|
* @return ForeignKey |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getLocalKey() { |
|
107
|
|
|
return $this->lk; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getRelatedName() { |
|
111
|
|
|
$relatedName = $this->lk->getRefPhpName(); |
|
112
|
|
|
if (empty($relatedName)) { |
|
113
|
|
|
$relatedName = $this->foreign->getPhpName(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $relatedName; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getReverseRelatedName() { |
|
120
|
|
|
$reverseRelatedName = $this->fk->getRefPhpName(); |
|
121
|
|
|
if (empty($reverseRelatedName)) { |
|
122
|
|
|
$reverseRelatedName = $this->model->getPhpName(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $reverseRelatedName; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|