More

    Magento Installation Scripts – Part 3: Installing New Tables (coolryan.com)


    Continuing on from Part 2, we will be installing a table with our install script.  It will be a simple table with some columns and a primary key.

    Let’s walk through this line by line.

    This retrieves the connection class we are using.

    This is the name for our table that we will be constructing.  The getTable()  is a reference call to the table name defined in our config.xml.  Speaking of which, let’s go ahead and define that now:

    If you look under the <entities>  node, you will see a node called <content> .  That is the alias for our defined table name under the node <table> .  So our table name will be content_table.

    The addColumn()  method accepts 5 parameters.  This method is defined in theVarien_Db_Ddl_Table  class.

    1. ‘content_id’ is the name of our column.
    2. Varien_Db_Ddl_Table::TYPE_SMALLINT  is the column type.
    3. null (in this case) is the size of the column.  Can be anything from 255 to 2M.  Accepts strings and integers.
    4. array(…) is the options that are available.  Available options are:
      • ‘unsigned’, for number types only. Default: FALSE.
      • ‘precision’, for numeric and decimal only. Default: taken from $size, if not set there then 0.
      • ‘scale’, for numeric and decimal only. Default: taken from $size, if not set there then 10.
      • ‘default’. Default: not set.
      • ‘nullable’. Default: TRUE.
      • ‘primary’, add column to primary index. Default: do not add.
      • ‘primary_position’, only for column in primary index. Default: count of primary columns + 1.
      • ‘identity’ or ‘auto_increment’. Default: FALSE.
    5. ‘Content ID’ is the comment for that particular column.  This is usually just a general description of what the column data is.

    As far as column types go, you can use:

    • TYPE_BOOLEAN = ‘boolean’
    • TYPE_SMALLINT = ‘smallint’
    • TYPE_INTEGER = ‘integer’
    • TYPE_BIGINT = ‘bigint’
    • TYPE_FLOAT = ‘float’
    • TYPE_NUMERIC = ‘numeric’
    • TYPE_DECIMAL = ‘decimal’
    • TYPE_DATE = ‘date’
    • TYPE_TIMESTAMP = ‘timestamp’
    • TYPE_DATETIME = ‘datetime’
    • TYPE_TEXT = ‘text’
    • TYPE_BLOB = ‘blob’
    • TYPE_VARBINARY = ‘varbinary’

    This is the overall table description.
    So far, all this has done is create the structure for your table.  It stores this structure inside the $table  variable. The next part actually creates the table itself.

    There you have it.  Now you have installed a table with your installation script!

    Overview : Magento Installation Scripts (Cool Ryan) 
    Previous :Part 2: Your First Script
    Next : Part 4: EAV Tables

    Post from coolryan.com

    Recent Articles

    spot_img

    Related Stories

    Stay on op - Ge the daily news in your inbox