I have just started to build my first plugin with Craft CMS.

Im at the part where I want to defne my database table.


For the method definedAttributes() what values will the array accept.

It looks like I can define values and types, but how do i add field lengths, primarykeys etc..

Are there some other docs that I can look at?

Thanks for your help.

1 Answer 1


You best bet would be to start by knowing all the exisiting default attribute types in this file:


Then look in

craft/app/helpers/ModelHelper.php and craft/app/helpers/DbHelper.php

There you will find all sorts of properties for column types and their default configs options like maxLength. Here is all the ColumnTypes defaults:

public static $attributeTypeDefaults = array(
    AttributeType::Mixed      => array('model' => null, 'column' => ColumnType::Text),
    AttributeType::Bool       => array('maxLength' => 1, 'default' => false, 'required' => true, 'column' => ColumnType::TinyInt, 'unsigned' => true),
    AttributeType::ClassName  => array('maxLength' => 150, 'column' => ColumnType::Varchar),
    AttributeType::DateTime   => array('column' => ColumnType::DateTime),
    AttributeType::Email      => array('minLength' => 5, 'column' => ColumnType::Varchar),
    AttributeType::Enum       => array('values' => array(), 'column' => ColumnType::Enum),
    AttributeType::Handle     => array('maxLength' => 255, 'reservedWords' => 'id,dateCreated,dateUpdated,uid,title', 'column' => ColumnType::Varchar),
    AttributeType::Locale     => array('column' => ColumnType::Locale),
    AttributeType::Name       => array('maxLength' => 255, 'column' => ColumnType::Varchar),
    AttributeType::Number     => array('min' => null, 'max' => null, 'decimals' => 0),
    AttributeType::SortOrder  => array('column' => ColumnType::TinyInt),
    AttributeType::Template   => array('maxLength' => 500, 'column' => ColumnType::Varchar),
    AttributeType::Url        => array('maxLength' => 255, 'column' => ColumnType::Varchar),

You will also see in the DbHelper.php how the column types map into the mysql definition of the table like so:

case ColumnType::Decimal:
                $def = 'DECIMAL('.$config['length'].','.$config['decimals'].')';

Hope that helps. I would encourage you to look at all the defineAttributes() methods in the craft/app/records directory.

  • Thanks Luke thats a big help, how do I get my PK field to auto increament? I have noticed that a uid field gets added how is this used? Jan 15, 2015 at 12:15
  • No worries. I suppose it should be documented better but most of the defaults are fine anyway so just using the default attribute types is fine. Jan 15, 2015 at 12:24
  • PK id already auto increments by default you don't need to add id to defineAttributes it gets added by craft on install. Jan 15, 2015 at 14:48
  • 1
    @GaryConstable Craft adds and uses the uid column internally. No need to worry about it.
    – Brad Bell
    Jan 15, 2015 at 16:55
  • 1
    I'm late to the party, but I'm also working through validation/records etc. I found this article to be quite helpful as well.
    – Damon
    Mar 27, 2015 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.