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.

1 Answers1

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.

Luke Holder
  • 6,827
  • 14
  • 27
  • 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
  • 1
    @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
  • For further reference, here's a list of all the possible AttributeType::* constants (the above list from ModelHelper.php doesn't include all the possibilities): https://github.com/pixelandtonic/Craft-Release/blob/master/app/enums/AttributeType.php – Phil Gyford Sep 02 '15 at 18:07