-
Notifications
You must be signed in to change notification settings - Fork 6
/
EavAttribute.php
493 lines (418 loc) · 15.7 KB
/
EavAttribute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
/**
* EavAttribute class file
* @author Igor Manturov, Jr. <[email protected]>
* @link https://github.com/iAchilles/eavactiverecord/
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/
/**
* EavAttribute class represents methods to manipulate EAV attributes (creating a new attribute, updating an existing
* attribute, removing an attribute).
*
* There are two types of EAV attributes: a multivalued attribute and single valued attribute. The multivalued
* attribute can have more than one value at a time for an attribute. The single valued attribute can hold only single
* value at a time.
* <pre>
* $attribute1 = new EavAttribute();
* $attribute1->type = EavAttribute::TYPE_SINGLE; //Defines the attribute type. This attribute can hold only single value.
*
* $attribute2 = new EavAttribute();
* $attribute2->type = EavAttribute::TYPE_MULTIPLE; //This attribute can hold multiple values.
* </pre>
*
* There are six data types of EAV attribute values (surely, you can create own data types): 'IntDataType', 'VarcharDataType',
* 'DatetimeDataType', 'TextDataType', 'MoneyDataType' and 'NumericDataType' (the last two data types were added since version 1.0.3).
* The name of the data type must be equal to a class name that is derived from
* the class EavValue. The value of the EAV attribute is stored as a record in a table that is based on the attribute
* data type. It uses separate tables for each data type.
* If the value of the attribute must be stored in an integer, you must use the constant EavAttribute::DATA_TYPE_INT
* to assign a value to the property EavAttribute::$data_type:
* <pre>
* $attribute = new EavAttribute();
* $attribute->data_type = EavAttribute::DATA_TYPE_INT; //Values of this attribute will be stored in an integer.
* </pre>
* To specify a data type of an attribute you can use constants EavAttribute::DATA_TYPE_INT ('IntDataType'),
* EavAttribute::DATA_TYPE_DATETIME ('DatetimeDataType'), EavAttribute::DATA_TYPE_TEXT ('TextDataType'),
* EavAttribute::DATA_TYPE_VARCHAR ('VarcharDataType'), EavAttribute::DATA_TYPE_NUMERIC ('NumericDataType'),
* EavAttribute::DATA_TYPE_MONEY ('MoneyDataType').
*
* The name of the EAV-attribute must be unique and follow PHP variable naming convention
* (http://php.net/manual/en/language.variables.basics.php). The following name of the attribute is invalid:
* <pre>
* $attribute = new EavAttribute();
* $attribute->name = 2; //Invalid name
* $attribute->name = '3abc'; //Invalid name
* </pre>
*
* The following name of the attribute is correct:
* <pre>
* $attribute = new EavAttribute();
* $attribute->name = 'abc3'; //Correct name
* $attribute->name = '_a2c'; //Correct name
* </pre>
*
* When you create a new EAV-attribute you also can determine validation rules by calling the method EavAttribute::setRules().
* The following code fragment shows how to add validation rules to an attribute:
* <pre>
* $rules = array('length' => array('min' => 3, 'max' => 25), 'required' => array('on' => 'register'));
* $attribute = new EavAttribute();
* $attribute->setRules($rules);
* </pre>
* Note, if an attribute does not contain validation rules so that it cannot be massively assigned.
*
* The following is a complete code of creating a new attribute:
* <pre>
* $attribute = new EavAttribute();
* $attribute->name = 'age';
* $attribute->label = 'Your age';
* $attribute->type = EavAttribute::TYPE_SINGLE;
* $attribute->data_type = EavAttribute::DATA_TYPE_INT;
* $attribute->setRules(array('numeric' => array('min' => 18, 'max' => 100, 'integerOnly' => true), 'required'));
* $attribute->save();
* </pre>
*
*
* @property integer $id Primary key.
* @property integer $type The attribute type. If the attribute may hold multiple values it must be set to 1. If the attribute
* may only hold a single value it must be set to 0. You can use constants EavAttribute::TYPE_SINGLE and
* EavAttribute::TYPE_MULTIPLE to assign a value to this property.
* @property string $data_type The attribute value data type. It must contain a name of a class that is derived from
* the class EavValue. You can use constants EavAttribute::DATA_TYPE_INT, EavAttribute::DATA_TYPE_DATETIME,
* EavAttribute::DATA_TYPE_TEXT, EavAttribute::DATA_TYPE_VARCHAR, EavAttribute::DATA_TYPE_NUMERIC and
* EavAttribute::DATA_TYPE_MONEY to assign a value to this property.
* @property string $name The attribute name. Must be unique and follow PHP variable naming convention.
* @property string $label The attribute label.
* @property string $data Serialized data is stored and recovered using PHP's serialize() and unserialize() functions.
* DO NOT set the value of this property directly.
*
* @since 1.0.0
*/
class EavAttribute extends CActiveRecord implements Serializable
{
const DATA_TYPE_INT = 'IntDataType';
const DATA_TYPE_VARCHAR = 'VarcharDataType';
const DATA_TYPE_DATETIME = 'DatetimeDataType';
const DATA_TYPE_TEXT = 'TextDataType';
const DATA_TYPE_NUMERIC = 'NumericDataType';
const DATA_TYPE_MONEY = 'MoneyDataType';
const TYPE_SINGLE = 0;
const TYPE_MULTIPLE = 1;
const CACHE_PREFIX = 'eav';
const CACHE_ID = 'eavCache';
public $unserializedObject;
private $unserializedData;
private $oldDataType;
private static $cache;
public function serialize()
{
if ($this->getIsNewRecord())
{
return null;
}
return serialize($this->getAttributes());
}
public function unserialize($serialized)
{
$attributes = unserialize($serialized);
$this->unserializedObject = self::model()->populateRecord($attributes);
}
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '{{eav_attribute}}';
}
public function init()
{
if ($this->getIsNewRecord())
{
$this->setUnserializedData(array(
'rules' => array(),
));
}
}
public function rules()
{
return array(
array('name, type, data_type', 'required'),
array('name', 'match', 'pattern' => '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/'),
array('data_type', 'eavAttributeDataTypeValidator'),
array('type', 'in', 'range' => array(self::TYPE_MULTIPLE, self::TYPE_SINGLE)),
array('label', 'safe'),
);
}
protected function afterFind()
{
$this->oldDataType = $this->data_type;
}
protected function beforeSave()
{
$this->serializeData();
return parent::beforeSave();
}
protected function afterSave()
{
parent::afterSave();
$this->setIsNewRecord(false);
$this->setCacheEavAttribute($this);
}
/**
* Sets the list of possible values for the attribute. E.g. it can be used to create a drop-down list.
* <pre>
* $values = array(1 => 'One', 2 => 'Two');
* $attribute->setPossibleValues($values);
* </pre>
* @param array $values List of possible values (an associative array of value=>label pairs).
* @throws CException Passed wrong type of argument
* @since Version 1.0.2
*/
public function setPossibleValues($values)
{
if (!is_array($values))
{
throw new CException('Argument 1 passed to ' . __METHOD__
. '() must be an array.');
}
else
{
$data = $this->getUnserializedData();
$data['values'] = $values;
$this->setUnserializedData($data);
}
}
/**
* Returns the list of possible values for the attribute.
* @return array The list of possible values.
* @since Version 1.0.2
*/
public function getPossibleValues()
{
$data = $this->getUnserializedData();
return isset($data['values']) ? $data['values'] : array();
}
/**
* Adds validation rules for the EAV attribute.
* <pre>
* $rules = array(
* 'length' => array('max' => 5, 'min' => 2),
* 'date' => array('format' => 'yyyy-M-d H:m:s'),
* );
* $attribute->setRules($rules);
* </pre>
* @param array $rules An array that contains nested arrays that are indexed by the name of a validator.
* Each nested array contains the definition of a validation rule.
* @throws CException Passed wrong type of argument.
*/
public function setRules($rules)
{
if (!is_array($rules))
{
throw new CException('Argument 1 passed to ' . __METHOD__
. '() must be an array.');
}
foreach ($rules as $key => $value)
{
if (is_int($key))
{
unset($rules[$key]);
$rules[$value] = array();
}
}
$data = $this->getUnserializedData();
$data['rules'] = $rules;
$this->setUnserializedData($data);
}
/**
* Returns all the validation rules that were defined for the EAV attribute.
* @return array All the validation rules that were defined for the EAV attribute. If no validation rules exist, an
* empty array is returned.
*/
public function getEavValidatorList()
{
$data = $this->getUnserializedData();
return isset($data['rules']) ? $data['rules'] : array();
}
/**
* Returns the list of instances of the class EavAttribute (indexed by the attribute name).
* @param array $names Names of EAV attributes whose instances should be returned.
* @return array The list of instances of the class EavAttribute (indexed by the attribute name).
* If attributes are not found, an empty array is returned.
* @throws CException Passed wrong type of argument.
*/
public function getEavAttributes($names)
{
if (!is_array($names))
{
throw new CException('Argument 1 passed to ' . __METHOD__
. '() must be an array.');
}
$attributes = array();
$keys = array();
foreach ($names as $name)
{
$attribute = $this->getCacheEavAttribute($name);
if ($attribute === false)
{
$keys[] = "'" . $name . "'";
continue;
}
$attributes[$name] = $attribute;
}
if (!empty($keys))
{
$condition = 'name IN (' . implode(', ', $keys) . ')';
$keys = self::model()->findAll($condition);
}
foreach ($keys as $attr)
{
$attributes[$attr->name] = $attr;
$this->setCacheEavAttribute($attr);
}
return $attributes;
}
/**
* Validates the specified value of the property EavAttribute::$data_type.
* @param $attribute
* @param $params
*/
public function eavAttributeDataTypeValidator($attribute, $params)
{
if (!$this->getIsNewRecord())
{
if ($this->oldDataType !== $this->data_type)
{
$this->addError($attribute, 'The value of the property ' . __CLASS__ . '::$' . $attribute . ' cannot be
changed if it was saved previously.');
}
}
else
{
if (!@class_exists($this->data_type))
{
$this->addError($attribute, 'The class ' . $this->data_type . ' not found.');
}
else
{
if (!(EavValue::model($this->data_type) instanceof EavValue))
{
$this->addError($attribute, 'The class ' . $this->data_type . '.php must be '
. 'a subclass of the class EavValue.');
}
}
}
}
/**
* Returns the value of the property EavAttribute::$unserializeData.
* @return array Unserialized data.
*/
protected function getUnserializedData()
{
if (is_null($this->unserializedData))
{
$this->setUnserializedData(unserialize($this->data));
}
return $this->unserializedData;
}
/**
* Sets the value of the property EavAttribute::$unserializeData.
* @param array $data Unserialized data.
*/
protected function setUnserializedData($data)
{
$this->unserializedData = $data;
}
/**
* Generates a storable representation of the property EavAttribute::$unserializeData.
*/
protected function serializeData()
{
$data = $this->getUnserializedData();
$this->data = serialize($data);
}
/**
* Saves the given EAV-attribute to the cache store.
* @param EavAttribute $attribute
* @return boolean true if the given EAV-attribute is successfully stored into cache,
* false otherwise.
*/
protected function setCacheEavAttribute(EavAttribute $attribute)
{
$duration = $this->getDbConnection()->schemaCachingDuration;
if ($duration > 0)
{
$key = $this->createCacheKey($attribute->name);
$cache = $this->getCache();
return $cache->set($key, serialize($attribute), $duration);
}
return false;
}
/**
* Retrieves a cached instance of the class EavAttribute.
* @param string $name The name of the attribute whose instance must be fetched from the cache.
* @return mixed An instance of the class EavAttribute, false otherwise.
*/
protected function getCacheEavAttribute($name)
{
$duration = $this->getDbConnection()->schemaCachingDuration;
if ($duration > 0)
{
$cache = $this->getCache();
$key = $this->createCacheKey($name);
$cached = $cache->get($key);
if ($cached === false)
{
return false;
}
$instance = unserialize($cache->get($key));
return $instance->unserializedObject;
}
return false;
}
/**
* Deletes an instance of the class EavAttribute with the given name from the cache.
* @param string $name The name of the attribute whose instance must be removed from the cache.
* @return boolean true if no error happens during deletion.
*/
protected function deleteCacheEavAttribute($name)
{
$cache = $this->getCache();
$key = $this->createCacheKey($name);
return $cache->delete($key);
}
/**
* Returns the cache component.
* @return ICache An instance of a class that implements the interface ICache.
* @throws CException If the cache component is not initialized.
*/
protected function getCache()
{
if (!is_null(self::$cache))
{
return self::$cache;
}
if (isset(Yii::app()->{self::CACHE_ID})
&& Yii::app()->{self::CACHE_ID} instanceof ICache)
{
self::$cache = Yii::app()->{self::CACHE_ID};
return self::$cache;
}
$id = $this->getDbConnection()->schemaCacheID;
if (isset(Yii::app()->$id) && Yii::app()->$id instanceof ICache)
{
self::$cache = Yii::app()->$id;
return self::$cache;
}
throw new CException('The cache component is not initialized and cannot be read.');
}
/**
* Generates a cache key name for the specified attribute name.
* @param string $name Attribute name.
* @return string A cache key name.
*/
protected function createCacheKey($name)
{
return self::CACHE_PREFIX . '_' . $name;
}
}