\Dewdrop\Db\EavDefinition

This class handles an EAV definition attached to a \Dewdrop\Db\Table object. The EAV definition can load and save values for an EAV field, retrieve the list of available attributes, optionally filtered (e.g.

to a single user account's attribute), etc.

To use EAV, you should add an attribute table and a set of value tables to your database. For the following example, assume the table the EAV is connected to in your database is called "widgets". By convention, the attribute table follows this format:

widgets_attributes

The value tables should be named like this:

widgets_eav_values_varchar
widgets_eav_values_text
widgets_eav_values_datetime
widgets_eav_values_decimal
widgets_eav_values_int
widgets_eav_values_boolean

You can modify the "_eavvalues" portion of the value table names by changing the $valueTablePrefix property.

If you'd like to just stick with the Dewdrop naming conventions, you can generate the EAV tables using the "gen-eav" CLI command.

Summary

Methods
Properties
Constants
__construct()
setOptions()
setAttributeTableName()
setValueTablePrefix()
setAttributeFilterCallback()
getAttributeTableName()
hasAttribute()
setRequiredIndex()
getRequiredIndex()
setDeletedIndex()
getFieldMetadata()
getAttributes()
getAttribute()
save()
loadInitialValue()
augmentSelect()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
getBackendTypeTableName()
loadAttributes()
detectGenericType()
$table
$attributeTableName
$valueTablePrefix
$attributeFilterCallback
$attributes
$requiredIndex
$deletedIndex
N/A

Properties

$table

$table : \Dewdrop\Db\Table

The Table object this EAV definition is associated with.

Type

\Dewdrop\Db\Table

$attributeTableName

$attributeTableName : string

The name of the table where EAV attributes are stored. Defaults to the name of the table that this EAV definition was registered to with a suffix of "_attributes".

Type

string

$valueTablePrefix

$valueTablePrefix : string

The prefix that will be used for value table names. The value table's full name is composed of the name of the table that registered this EAV definition, then this prefix, and then the backend type of the attribute that is being saved.

Type

string

$attributeFilterCallback

$attributeFilterCallback : mixed

A callback that can be used to filter the attribute list for this EAV definition. If, for example, you have a SaaS application where each account holder is able to create their own custom fields, you might want to filter this attribute list like this:

$table->getEav()->setAttributeFilterCallback(
    function ($stmt) use ($currentUser) {
        $stmt->where(
            'account_id = ?',
            $currentUser->get('account_id')
        );
    }
);

Type

mixed

$attributes

$attributes : array

The available attributes for this EAV definition. This is lazy-loaded the first time you attempt to call any attribute related method.

Type

array

$requiredIndex

$requiredIndex : string

The index in the attribute definition that determines whether the field is required.

Type

string

$deletedIndex

$deletedIndex : string

The name of a column in the attributes DB table that will be true when the attribute should be treated as if it was deleted.

Type

string

Methods

__construct()

__construct(\Dewdrop\Db\Table  $table, array  $options = array()) 

Register a new EAV definition to supplied table and set any additional options specified in the supplied array.

Parameters

\Dewdrop\Db\Table $table
array $options

setOptions()

setOptions(array  $options) : \Dewdrop\Db\Eav\Definition

Set multiple options using the supplied array of option name and value pairs.

Parameters

array $options

Returns

\Dewdrop\Db\Eav\Definition

setAttributeTableName()

setAttributeTableName(string  $attributeTableName) : \Dewdrop\Db\Eav\Definition

Set the name of the table where you want to store attributes (not values, but the actual rules/metadata for the attribute itself) for this EAV definition.

Parameters

string $attributeTableName

Returns

\Dewdrop\Db\Eav\Definition

setValueTablePrefix()

setValueTablePrefix(string  $valueTablePrefix) : \Dewdrop\Db\Eav\Definition

Set the prefix that will come before the backend type in a value table's name.

Parameters

string $valueTablePrefix

Returns

\Dewdrop\Db\Eav\Definition

setAttributeFilterCallback()

setAttributeFilterCallback(mixed  $attributeFilterCallback) : \Dewdrop\Db\Eav\Definition

Set a callback for filtering the initial \Dewdrop\Db\Select object that retrieves the list of available attributes. This can be any valid PHP callable (i.e. function name as string, anonymous function, or method array). The function should return the modified \Dewdrop\Db\Select.

Parameters

mixed $attributeFilterCallback

Returns

\Dewdrop\Db\Eav\Definition

getAttributeTableName()

getAttributeTableName() : string

Get the name of the table where attributes are stored. If none is set, we use the name of the table this EAV definition is registered to followed by "_attributes".

Returns

string

hasAttribute()

hasAttribute(string  $name) : boolean

Check to if there is an attribute with the supplied name.

Parameters

string $name

Returns

boolean

setRequiredIndex()

setRequiredIndex(string  $requiredIndex) : $this

Set the index of the column in the attributes table that will be used to determine if an attribute is required or not. Useful if you require an attribute in some contexts but not others (e.g. you have a column called "is_required_public" and another called "is_required_admin" and need to consult a different column depending upon who is logged in).

Parameters

string $requiredIndex

Returns

$this

getRequiredIndex()

getRequiredIndex() : string

Get the index/name of the column in the attribute definition that tells us if a value is required.

Returns

string

setDeletedIndex()

setDeletedIndex(string  $deletedIndex) : $this

Set the index of the column in the attributes table that will be used to determine if an attribute has been deleted or not. Useful when end-users can delete attributes via some kind of GUI.

Parameters

string $deletedIndex

Returns

$this

getFieldMetadata()

getFieldMetadata(string  $name) : array

Get a \Dewdrop\Db\Adapter::describeTable() compatible metadata definition.

This allows EAV field's to integrate nicely with the standard Field API.

Parameters

string $name

Returns

array

getAttributes()

getAttributes() : array

Get the full set of attributes supported by this definition. The array's keys are the attribute field names (e.g. "eav_1" or "eav_22") and the values are the information from the attributes table in the DB.

Returns

array

getAttribute()

getAttribute(string  $name) : array

Get information about the attribute matching the supplied name.

Parameters

string $name

Returns

array

save()

save(string  $name, mixed  $value, array  $pkeyValues) 

Save the value for the supplied attribute, using the primary key values from the table this EAV definition is registered to.

Parameters

string $name

The name of the attribute to save (e.g. "eav_1")

mixed $value
array $pkeyValues

loadInitialValue()

loadInitialValue(\Dewdrop\Db\Row  $row, string  $name) 

Load the initial value for the supplied attribute name. If the supplied row is new (i.e. no primary key value), then we use the default value for the attribute. Otherwise, we query that appropriate value table.

Parameters

\Dewdrop\Db\Row $row
string $name

augmentSelect()

augmentSelect(\Dewdrop\Db\Select  $select) : \Dewdrop\Db\Select

Augment the provided Select object with all the EAV attribute values from this definition.

Parameters

\Dewdrop\Db\Select $select

Throws

\Dewdrop\Db\Select

Returns

\Dewdrop\Db\Select

getBackendTypeTableName()

getBackendTypeTableName(string  $backendType) : string

Assemble the name of the value table for the supplied backend type.

Parameters

string $backendType

Returns

string

loadAttributes()

loadAttributes() : array

Load the list of available attributes from the database, if they haven't been loaded already. The array of attributes will use the attribute field name (e.g. "eav_1") for a key.

Returns

array

detectGenericType()

detectGenericType(string  $eavType) : string

Determine a generic data type appropriate for the supplied EAV type.

The EAV backend value tables are named after the data types in the DB, so they don't directly match generic types from our PHP API. This method maps the two domains together so that we can check an EAV field's generic type in our application code.

Parameters

string $eavType

Throws

\Dewdrop\Exception

Returns

string