1 | <?php |
||
39 | abstract class ActiveRecord extends \craft\db\ActiveRecord |
||
40 | { |
||
41 | use DateCreatedRulesTrait, |
||
42 | DateUpdatedRulesTrait, |
||
43 | UidRulesTrait; |
||
44 | |||
45 | /** |
||
46 | * These attributes will have their 'getter' methods take priority over the normal attribute lookup. It's |
||
47 | * VERY important to note, that the value returned from the getter should NEVER be different than the raw |
||
48 | * attribute value set. If, for whatever reason, the getter determines the attribute value is |
||
49 | * incorrect, it should set the new value prior to returning it. |
||
50 | * |
||
51 | * These getters are commonly used to ensure an associated model and their identifier are in sync. For example, |
||
52 | * a userId attribute and a user object (with an id attribute). An operation may have saved and set an Id on the |
||
53 | * model, but the userId attribute remains null. The getter method may check (and set) the value prior to |
||
54 | * returning it. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $getterPriorityAttributes = []; |
||
59 | |||
60 | /** |
||
61 | * These attributes will have their 'setter' methods take priority over the normal attribute setting. |
||
62 | * |
||
63 | * These setters are commonly used to ensure an associated model and their identifier are in sync. For example, |
||
64 | * a userId attribute and a user object (with an id attribute). |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $setterPriorityAttributes = []; |
||
69 | |||
70 | /** |
||
71 | * The table alias |
||
72 | */ |
||
73 | const TABLE_ALIAS = ''; |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public static function tableAlias() |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public static function tableName() |
||
90 | |||
91 | |||
92 | /******************************************* |
||
93 | * OVERRIDE CONDITION HANDLING |
||
94 | *******************************************/ |
||
95 | |||
96 | /** |
||
97 | * Finds ActiveRecord instance(s) by the given condition. |
||
98 | * This method is internally called by [[findOne()]] and [[findAll()]]. |
||
99 | * @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter |
||
100 | * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. |
||
101 | * @throws InvalidConfigException if there is no primary key defined. |
||
102 | * @internal |
||
103 | */ |
||
104 | protected static function findByCondition($condition) |
||
105 | { |
||
106 | $query = static::find(); |
||
107 | |||
108 | if (!empty($condition) && !ArrayHelper::isAssociative($condition)) { |
||
109 | // query by primary key |
||
110 | $primaryKey = static::primaryKey(); |
||
111 | if (isset($primaryKey[0])) { |
||
112 | $pk = $primaryKey[0]; |
||
113 | if (!empty($query->join) || !empty($query->joinWith)) { |
||
114 | $pk = static::tableName() . '.' . $pk; |
||
115 | } |
||
116 | // if condition is scalar, search for a single primary key, if it is array, search for |
||
117 | // multiple primary key values |
||
118 | $condition = [$pk => is_array($condition) ? array_values($condition) : $condition]; |
||
119 | } else { |
||
120 | throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.'); |
||
121 | } |
||
122 | } elseif (is_array($condition)) { |
||
123 | foreach ($condition as $key => $value) { |
||
124 | if ($query->canSetProperty($key)) { |
||
125 | $query->{$key} = $value; |
||
126 | unset($condition[$key]); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** @noinspection PhpInternalEntityUsedInspection */ |
||
131 | $condition = static::filterCondition($condition); |
||
132 | } |
||
133 | |||
134 | return $query->andWhere($condition); |
||
135 | } |
||
136 | |||
137 | |||
138 | /******************************************* |
||
139 | * FIND |
||
140 | *******************************************/ |
||
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | public static function findOne($condition) |
||
153 | |||
154 | |||
155 | /******************************************* |
||
156 | * GET |
||
157 | *******************************************/ |
||
158 | |||
159 | /** |
||
160 | * @param $condition |
||
161 | * @return static |
||
162 | * @throws RecordNotFoundException |
||
163 | */ |
||
164 | public static function getOne($condition) |
||
179 | |||
180 | /** |
||
181 | * @param $condition |
||
182 | * @return static[] |
||
183 | * @throws RecordNotFoundException |
||
184 | */ |
||
185 | public static function getAll($condition) |
||
200 | |||
201 | |||
202 | /******************************************* |
||
203 | * RULES |
||
204 | *******************************************/ |
||
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | public function rules() |
||
218 | |||
219 | /******************************************* |
||
220 | * ATTRIBUTES |
||
221 | *******************************************/ |
||
222 | |||
223 | /** |
||
224 | * @param string $name |
||
225 | * @return mixed |
||
226 | */ |
||
227 | public function __get($name) |
||
235 | |||
236 | /** |
||
237 | * @param string $name |
||
238 | * @param mixed $value |
||
239 | */ |
||
240 | public function __set($name, $value) |
||
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | */ |
||
253 | public function getDirtyAttributes($names = null) |
||
266 | |||
267 | /** |
||
268 | * @param $name |
||
269 | * @return bool |
||
270 | */ |
||
271 | protected function hasSetterPriority($name) |
||
276 | |||
277 | /** |
||
278 | * @param $name |
||
279 | * @return bool |
||
280 | */ |
||
281 | protected function hasGetterPriority($name) |
||
286 | } |
||
287 |