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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
// app/code/local/Namespace/Modulename/sql/module_setup/install-1.0.0.php
< ?php $installer = $this; $installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable(‘modulename/content’))
->addColumn(‘content_id’, Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
‘identity’ => true,
‘nullable’ => false,
‘primary’ => true,
), ‘Content ID’)
->addColumn(‘name’, Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
‘nullable’ => false,
), ‘Content Name’)
->addColumn(‘slug’, Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
‘nullable’ => false,
), ‘unique slug’)
->addColumn(‘content’, Varien_Db_Ddl_Table::TYPE_TEXT, ‘2M’, array(
), ‘Content’)
->addColumn(‘creation_time’, Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
), ‘Creation Time’)
->addColumn(‘update_time’, Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
), ‘Modification Time’)
->addColumn(‘is_active’, Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
‘nullable’ => false,
‘default’ => ‘1’,
), ‘Is This Active?’)
->setComment(‘Module Table’);
$installer->getConnection()->createTable($table);
$installer->endSetup();
|
Let’s walk through this line by line.
1
|
$installer->getConnection()
|
This retrieves the connection class we are using.
1
|
...->newTable($installer->getTable(‘modulename/content’))
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!– app/code/local/Namespace/Modulename/etc/config.xml –>
…
<global>
<models>
<modulename>
<class>Namespace_Modulename_Model</class>
<resourceModel>module_resource</resourceModel>
</modulename>
<module_resource>
<class>Namespace_Modulename_Model_Resource</class>
<entities>
<content>
<table>content_table</table>
</content>
</entities>
</module_resource>
</models>
</global>
...
|
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.
1
2
3
4
5
|
...->addColumn(‘content_id’, Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
‘identity’ => true,
‘nullable’ => false,
‘primary’ => true,
), ‘Content ID’)
|
The addColumn() method accepts 5 parameters. This method is defined in theVarien_Db_Ddl_Table class.
- ‘content_id’ is the name of our column.
- Varien_Db_Ddl_Table::TYPE_SMALLINT is the column type.
- null (in this case) is the size of the column. Can be anything from 255 to 2M. Accepts strings and integers.
- 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.
- ‘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’
1
|
...->setComment(‘Module Table’);
|
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.
1
|
$installer->getConnection()->createTable($table);
|
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