Defining Attributes

Once EAV behavior is attached to your table, you can now start defining virtual columns. There are two ways of defining virtual columns, CLI based or php-script based. We’ll explain how to define such columns using both methods.

Using PHP Script

Warning

You should do this step just once, otherwise you will end unnecessary updating columns every time the script is executed.

You can create new virtual columns definitions using the addColumn() method of your table, this method will update column information if already exists:

use Cake\ORM\Table;

class UsersTable extends Table
{
    public function initialize(Table $table)
    {
        $this->addBehavior('Eav.Eav');
        // WARNING: just run once these two lines
        $this->addColumn('user-age', ['type' => 'integer']);
        $this->addColumn('user-address', ['type' => 'string', 'bundle' => 'admin']);
    }
}

The first argument is the name of the column your are defining, you must use lower case letters, numbers or “-” symbol. For instance, user-age is a valid column name but user_age or User-Age are not.

And second argument is used to define column’s metadata and supports the following keys:

  • type (string): Type of data for that attribute, note that using any other type not listed here will throw an exception. Supported values are:
    • biginteger
    • binary
    • date
    • float
    • decimal
    • integer
    • time
    • datetime
    • timestamp
    • uuid
    • string
    • text
    • boolean
  • bundle (string): Indicates the attribute belongs to a bundle name within the table, check the “Bundles” section for further information. Defaults to null (no bundle).
  • searchable (bool): Whether this attribute can be used in SQL’s “WHERE” clauses. Defaults to true