1 | <?php |
||
27 | trait RelatedEnvironmentsAttribute |
||
28 | { |
||
29 | use ActiveRecordTrait; |
||
30 | |||
31 | /** |
||
32 | * Populates the named relation with the related records. |
||
33 | * Note that this method does not check if the relation exists or not. |
||
34 | * @param string $name the relation name, e.g. `orders` for a relation |
||
35 | * defined via `getOrders()` method (case-sensitive). |
||
36 | * @param ActiveRecordInterface|array|null $records the related records to be |
||
37 | * populated into the relation. |
||
38 | * @see getRelation() |
||
39 | */ |
||
40 | abstract public function populateRelation($name, $records); |
||
41 | |||
42 | /** |
||
43 | * Adds a new error to the specified attribute. |
||
44 | * @param string $attribute attribute name |
||
45 | * @param string $error new error message |
||
46 | */ |
||
47 | abstract public function addError($attribute, $error = ''); |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | public $autoSaveEnvironments = true; |
||
53 | |||
54 | /** |
||
55 | * Environments that are temporarily set during the save process |
||
56 | * |
||
57 | * @var null|array |
||
58 | */ |
||
59 | private $insertEnvironments; |
||
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | */ |
||
64 | abstract protected static function environmentRecordClass(): string; |
||
65 | |||
66 | /** |
||
67 | * @param array $config |
||
68 | * @return array |
||
69 | */ |
||
70 | protected abstract function prepareEnvironmentRecordConfig(array $config = []): array; |
||
71 | |||
72 | /** |
||
73 | * @return ActiveQueryInterface |
||
74 | */ |
||
75 | protected abstract function environmentRelationshipQuery(): ActiveQueryInterface; |
||
76 | |||
77 | /** |
||
78 | * Get all of the associated environments. |
||
79 | * |
||
80 | * @param array $config |
||
81 | * @return \yii\db\ActiveQueryInterface|\yii\db\ActiveQuery |
||
82 | */ |
||
83 | public function getEnvironments(array $config = []) |
||
96 | |||
97 | /** |
||
98 | * @param array $environments |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function setEnvironments(array $environments = []) |
||
129 | |||
130 | /** |
||
131 | * @param $environment |
||
132 | * @return ActiveRecord |
||
133 | */ |
||
134 | protected function resolveEnvironment($environment) |
||
160 | |||
161 | /** |
||
162 | * @param bool $force |
||
163 | * @return bool |
||
164 | * @throws \Throwable |
||
165 | * @throws \yii\db\StaleObjectException |
||
166 | */ |
||
167 | protected function saveEnvironments(bool $force = false): bool |
||
211 | |||
212 | /******************************************* |
||
213 | * EVENTS |
||
214 | *******************************************/ |
||
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | protected function beforeSaveEnvironments($insert): bool |
||
232 | |||
233 | /** |
||
234 | * We're extracting the environments that may have been explicitly set on the record. When the 'id' |
||
235 | * attribute is updated, it removes any associated relationships. |
||
236 | * |
||
237 | * @inheritdoc |
||
238 | * @throws \Throwable |
||
239 | */ |
||
240 | protected function insertInternalEnvironments($attributes = null) |
||
251 | |||
252 | /** |
||
253 | * @param null $attributes |
||
254 | * @return bool |
||
255 | * @throws \Throwable |
||
256 | * @throws \yii\db\StaleObjectException |
||
257 | */ |
||
258 | protected function upsertEnvironmentsInternal($attributes = null): bool |
||
270 | } |
||
271 |