1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dameter\abstracts; |
4
|
|
|
|
5
|
|
|
use yii\base\InvalidConfigException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ManyToManyModel |
9
|
|
|
* @property integer $order |
10
|
|
|
* @property DActiveRecord $parent |
11
|
|
|
* @property DActiveRecord $child |
12
|
|
|
* |
13
|
|
|
* @package dameter\abstracts |
14
|
|
|
* @author Tõnis Ormisson <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ManyToManyModel extends DActiveRecord |
17
|
|
|
{ |
18
|
|
|
public static $parentClass; |
19
|
|
|
public static $childClass; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function rules() |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
[['order'], 'required'], |
29
|
|
|
[['order'], 'integer'], |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public static function primaryKeySingle() |
37
|
|
|
{ |
38
|
|
|
/** @var DActiveRecord $parent */ |
39
|
|
|
$parent = static::$parentClass; |
40
|
|
|
/** @var DActiveRecord $child */ |
41
|
|
|
$child = static::$childClass; |
42
|
|
|
return $parent::tableName() . '_' . $child::tableName() . "_id"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
* @throws InvalidConfigException |
48
|
|
|
*/ |
49
|
|
|
public function init() |
50
|
|
|
{ |
51
|
|
|
parent::init(); |
52
|
|
|
if (is_null($this->parentModel) or is_null($this->childModel)) { |
|
|
|
|
53
|
|
|
throw new InvalidConfigException('Parent & Child class name must be defined for ' . static::class); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \yii\db\ActiveQuery |
59
|
|
|
*/ |
60
|
|
|
public function getParent() { |
61
|
|
|
/** @var DActiveRecord $className */ |
62
|
|
|
$className = static::$parentClass; |
63
|
|
|
return $this->hasOne($className); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return \yii\db\ActiveQuery |
68
|
|
|
*/ |
69
|
|
|
public function getChild() { |
70
|
|
|
/** @var DActiveRecord $className */ |
71
|
|
|
$className = static::$childClass; |
72
|
|
|
return $this->hasOne($className); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param DActiveRecord $child |
77
|
|
|
* @return array|DActiveRecord[]|\yii\db\ActiveRecord[] |
78
|
|
|
* @throws \yii\base\NotSupportedException |
79
|
|
|
*/ |
80
|
|
|
public static function getParents($child) |
81
|
|
|
{ |
82
|
|
|
/** @var DActiveRecord $className */ |
83
|
|
|
$className = static::$parentClass; |
84
|
|
|
return $className::find()->andWhere([$className::primaryKeySingle() => $child::primaryKeySingle()])->all(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param DActiveRecord $parent |
89
|
|
|
* @return array|DActiveRecord[]|\yii\db\ActiveRecord[] |
90
|
|
|
* @throws \yii\base\NotSupportedException |
91
|
|
|
*/ |
92
|
|
|
public static function getChildren($parent) |
93
|
|
|
{ |
94
|
|
|
/** @var DActiveRecord $className */ |
95
|
|
|
$className = static::$childClass; |
96
|
|
|
return $className::find()->andWhere([$className::primaryKeySingle() => $parent::primaryKeySingle()])->all(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |