6

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

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

http://buildwithcraft.com/docs/plugins/database

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.

9

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

craft/app/enums/AttributeType.php

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'].')';
                break;
            }

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

| improve this answer | |
  • 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? – Gary Constable Jan 15 '15 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. – Luke Holder Jan 15 '15 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. – Luke Holder Jan 15 '15 at 14:48
  • @GaryConstable Craft adds and uses the uid column internally. No need to worry about it. – Brad Bell Jan 15 '15 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 '15 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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