\Eav\Model\BehaviorEavBehavior

EAV Behavior.

Allows additional columns to be added to tables without altering its physical schema.

Usage:

$this->addBehavior('Eav.Eav');
$this->addColumn('user-age', ['type' => 'integer']);

Using virtual attributes in WHERE clauses:

$adults = $this->Users->find()
    ->where(['user-age >' => 18])
    ->all();

Summary

Methods
Properties
Constants
__construct()
addColumn()
beforeFind()
afterSave()
afterDelete()
attachEntityAttributes()
$Values
$Attributes
No constants found
_scopeQuery()
_virtualQuery()
_columnName()
_attributes()
_initModels()
_getAttributeNames()
_getEntityId()
_getType()
_getBundle()
_isSearchable()
_mapType()
$_tableAlias
$_schemaColumns
$_attributes
$_defaultConfig
N/A
No private methods found
No private properties found
N/A

Properties

$_tableAlias

$_tableAlias : string

Table alias.

Type

string

$_schemaColumns

$_schemaColumns : array

List of real column names of table.

Type

array

$_attributes

$_attributes : array

Attributes index by bundle, and by name within each bundle.

[
    'administrator' => [
        'admin-address' => [
            'type' => 'varchar',
            'searchable' => false
        ],
        'admin-phone' => [
            'type' => 'varchar',
            'searchable' => true
        ]
    ],
    'editor' => [
        'editor-last-login' => [
            'type' => 'datetime',
            'searchable' => false,
        ]
    ]
]

Type

array

$_defaultConfig

$_defaultConfig : array

Default configuration.

Type

array

Methods

__construct()

__construct(\Cake\ORM\Table $table, array $config)

Constructor.

Parameters

\Cake\ORM\Table $table

The table this behavior is attached to

array $config

Configuration array for this behavior

addColumn()

addColumn(string $name, array $options) : boolean

Registers a new EAV column or update if already exists.

Usage:

$this->Users->addColumn('user-age', [
    'type' => 'integer',
    'bundle' => 'some-bundle-name',
    'extra' => [
        'option1' => 'value1'
    ]
]);

Parameters

string $name

Column name. e.g. user-age

array $options

Column configuration options

Throws

\Cake\Error\FatalErrorException

When provided column name collides with existing column names

Returns

boolean —

True on success

beforeFind()

beforeFind(\Cake\Event\Event $event, \Cake\ORM\Query $query, \ArrayObject $options, boolean $primary) : boolean|null

Attaches virtual properties to entities.

This method iterates over each retrieved entity and invokes the attachEntityAttributes() method. This method should return the altered entity object with its virtual properties, however if this method returns FALSE the entity will be removed from the resulting collection. And if this method returns NULL will stop the find() operation.

This method is also responsible of looking for virtual columns in WHERE clause (if applicable) and properly scope the Query object. Query scoping is performed by the _scopeQuery() method.

Parameters

\Cake\Event\Event $event

The beforeFind event that was triggered

\Cake\ORM\Query $query

The original query to modify

\ArrayObject $options

Additional options given as an array

boolean $primary

Whether this find is a primary query or not

Returns

boolean|null

afterSave()

afterSave(\Cake\Event\Event $event, \Cake\Datasource\EntityInterface $entity, array $options) : void

After an entity is saved.

Parameters

\Cake\Event\Event $event

The event that was triggered

\Cake\Datasource\EntityInterface $entity

The entity that was saved

array $options

Additional options given as an array

afterDelete()

afterDelete(\Cake\Event\Event $event, \Cake\Datasource\EntityInterface $entity, array $options) : void

After an entity was removed from database. Here is when EAV values are removed from DB.

Parameters

\Cake\Event\Event $event

The event that was triggered

\Cake\Datasource\EntityInterface $entity

The entity that was deleted

array $options

Additional options given as an array

Throws

\Cake\Error\FatalErrorException

When using this behavior in non-atomic mode

attachEntityAttributes()

attachEntityAttributes(\Cake\Datasource\EntityInterface $entity, array $options) : boolean|null|\Cake\Datasource\EntityInterface

The method which actually fetches custom fields, invoked by `beforeFind()` for each entity in the collection.

  • Returning FALSE indicates the entity should be removed from the resulting collection.

  • Returning NULL will stop the entire find() operation.

Parameters

\Cake\Datasource\EntityInterface $entity

The entity where to fetch fields

array $options

Arguments given to beforeFind() method, possible keys are "event", "query", "options", "primary"

Returns

boolean|null|\Cake\Datasource\EntityInterface

_scopeQuery()

_scopeQuery(\Cake\ORM\Query $query, string|null $bundle) : \Cake\ORM\Query

Look for virtual columns in query's WHERE clause.

Parameters

\Cake\ORM\Query $query

The query to scope

string|null $bundle

Consider attributes only for a specific bundle

Returns

\Cake\ORM\Query —

The modified query object

_virtualQuery()

_virtualQuery(\Cake\Database\Expression\Comparison $expression, string|null $bundle) : \Cake\ORM\Query|boolean

Creates a sub-query for matching virtual fields.

Parameters

\Cake\Database\Expression\Comparison $expression

Expression to scope

string|null $bundle

Consider attributes only for a specific bundle

Returns

\Cake\ORM\Query|boolean —

False if not virtual field was found, or search feature was disabled for this field. The query object to use otherwise

_columnName()

_columnName(string $column) : string

Gets a clean column name from query expression.

Example:

$this->_columnName('Tablename.some_column');
// returns "some_column"

$this->_columnName('my_column');
// returns "my_column"

Parameters

string $column

Column name from query

Returns

string

_attributes()

_attributes(string|null $bundle) : array

Gets all attributes added to this table.

Parameters

string|null $bundle

Get attributes within given bundle, or all of them regardless of the bundle if not provided

Returns

array

_initModels()

_initModels() : void

Initializes Values and Attributes tables if there were not set before.

_getAttributeNames()

_getAttributeNames(string $bundle) : array

Gets a list of attribute names.

Parameters

string $bundle

Filter by bundle name

Returns

array

_getEntityId()

_getEntityId(\Cake\Datasource\EntityInterface $entity) : string

Calculates entity's primary key.

If PK is composed of multiple columns they will be merged with : symbol. For example, consider Users table with composed PK <nick, email>, then for certain User entity this method could return:

john-locke:john@the-island.com

Parameters

\Cake\Datasource\EntityInterface $entity

The entity

Returns

string

_getType()

_getType(string $attrName) : string

Gets attribute's EAV type.

Parameters

string $attrName

Attribute name

Returns

string —

Attribute's EAV type

_getBundle()

_getBundle(string $attrName) : string|null

Gets attribute's bundle.

Parameters

string $attrName

Attribute name

Returns

string|null

_isSearchable()

_isSearchable(string $attrName) : boolean

Whether the given attribute can be used in WHERE clauses.

Parameters

string $attrName

Attribute name

Returns

boolean

_mapType()

_mapType(string $type) : string

Maps schema data types to EAV's supported types.

  • datetime: "date", "time", "datetime"
  • decimal: "dec", "float", "decimal"
  • int: "integer", "int", "bool", "boolean"
  • text: "text"
  • varchar: "string", "varchar", "char"

Parameters

string $type

A schema type. e.g. "string", "integer"

Returns

string —

A EAV type. Possible values are datetime, decimal, int, text or varchar