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:
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
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.
$user = User::model()->findByPk(1); $user->getEavAttributes(array('attribute1', 'attribute2'));
getEavAttribute($attribute)
Get attribute value.
$user = User::model()->findByPk(1); $user->getEavAttribute('attribute1');
setEavAttribute($attribute, $value, $save = FALSE)
Set attribute value.
$user = User::model()->findByPk(1); $user->setEavAttribute('attribute1', 'value1');
setEavAttributes($attributes, $save = FALSE)
Set attributes values.
$user = User::model()->findByPk(1); $user->setEavAttributes(array('attribute1' => 'value1', 'attribute2' => 'value2'));
withEavAttributes($attributes)
Limits AR query to records with specified attributes.
$users = User::model()->withEavAttributes(array('skype'))->findAll(); $usersCount = User::model()->withEavAttributes(array('skype'))->count();
Changelog
0.5
[] Fixed error on get attribute without preload. [] Extension is now compatible only with Yii 1.1. [] Changed translation category to 'yiiext'. [] New naming conventions.
0.4
Warning: this version is not backwards compatible to 0.3.
[+] Added lazy-loading attributes. [+] Added caching attributes, set CacheId. [] Added additional boolean parameter $save for methods setEavAttribute, setEavAttributes, deleteEavAttributes, for save immediately. [+] Added method saveEavAttributes(). [+] Added method deleteEavAttributes(). [] Deleted findByEavAttributes() and findAllByEavAttributes() method. Use withEavAttributes(). [+] New syntax: $contacts = Contact::model()->withEavAttributes(array('skype' => 'yii'))->findAll(). [+] Added init test. [+] Added setAttributes method. [] setEavAttribute now return owner CActiveRecord. [] Fixed deleting eav-attributes after delete models. [] Fixed saving array of attribute values. [] Fixed saving secondary change for attribute value. [] Added translation for messages. [+] English readme. [] Corrected english descriptions. [] getEavAttribute now returns null if attribute is not defined. [] Minor code improvements.
0.3
[-] Deleted method readEavAttributes [+] Search model by eav-attribute set [+] All queries in DB will generated by CDbCriteria [] Method saveEavAttributes changed to saveEavAttribute to allow to save each attribute [] Method getModelTableFk now returns value of primary key. To get FK name use getModelTableFkField
0.2
[-] AR for each eav-attribute
0.1
[+] Initial public release.