-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathreadme_en.txt
103 lines (85 loc) · 2.46 KB
/
readme_en.txt
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
EAV behavior
============
Allows model to work with custom fields on the fly (EAV pattern).
Installing and configuring
--------------------------
### Create a table that will store EAV-attributes
SQL dump:
~~~
[sql]
CREATE TABLE IF NOT EXISTS `eavAttr` (
`entity` bigint(20) unsigned NOT NULL,
`attribute` varchar(250) NOT NULL,
`value` text NOT NULL,
KEY `ikEntity` (`entity`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
~~~
### Attach behaviour to your model
~~~
[php]
function behaviors() {
return array(
'eavAttr' => array(
'class' => 'ext.yiiext.behaviors.model.eav.EEavBehavior',
// Table that stores attributes (required)
'tableName' => 'eavAttr',
// model id column
// Default is 'entity'
'entityField' => 'entity',
// attribute name column
// Default is 'attribute'
'attributeField' => 'attribute',
// attribute value column
// Default is 'value'
'valueField' => 'value',
// Model FK name
// By default taken from primaryKey
'modelTableFk' => primaryKey,
// Array of allowed attributes
// All attributes are allowed if not specified
// Empty by default
'safeAttributes' => array(),
// Attribute prefix. Useful when storing attributes for multiple models in a single table
// Empty by default
'attributesPrefix' => '',
)
);
}
~~~
Methods
-------
### getEavAttributes($attributes)
Get attribute values indexed by attributes name.
~~~
[php]
$user = User::model()->findByPk(1);
$user->getEavAttributes(array('attribute1', 'attribute2'));
~~~
### getEavAttribute($attribute)
Get attribute value.
~~~
[php]
$user = User::model()->findByPk(1);
$user->getEavAttribute('attribute1');
~~~
### setEavAttribute($attribute, $value, $save = FALSE)
Set attribute value.
~~~
[php]
$user = User::model()->findByPk(1);
$user->setEavAttribute('attribute1', 'value1');
~~~
### setEavAttributes($attributes, $save = FALSE)
Set attributes values.
~~~
[php]
$user = User::model()->findByPk(1);
$user->setEavAttributes(array('attribute1' => 'value1', 'attribute2' => 'value2'));
~~~
### withEavAttributes($attributes)
Limits AR query to records with specified attributes.
~~~
[php]
$users = User::model()->withEavAttributes(array('skype'))->findAll();
$usersCount = User::model()->withEavAttributes(array('skype'))->count();
~~~