eav-behavior

Implements entity-attribute-value pattern.

  • 所有者: yiiext/eav-behavior
  • 平台:
  • 許可證:
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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();

主要指標

概覽
名稱與所有者yiiext/eav-behavior
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證
所有者活动
創建於2011-08-18 23:03:15
推送於2015-10-14 12:28:34
最后一次提交2015-02-27 00:14:25
發布數1
最新版本名稱v0.5 (發布於 2011-11-05 04:46:09)
第一版名稱v0.5 (發布於 2011-11-05 04:46:09)
用户参与
星數25
關注者數13
派生數15
提交數7
已啟用問題?
問題數10
打開的問題數7
拉請求數1
打開的拉請求數0
關閉的拉請求數1
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?