Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

We have provided a class that allows you create new database very easy. You don't need to care in case upgrade or fresh installation.

Create a new file like this:

Code Block
languagephp
titletable.php
linenumberstrue
<?php
namespace Apps\{your_app_id}\Installation\Database;

use \Core\App\Install\Database\Table as Table;
use \Core\App\Install\Database\Field as Field;

//class name must be the same with file name
class {your_class_name} extends Table
{
    /**
     *
     */
    protected function setTableName()
    {
        $this->_table_name = '{your_table_name}';
    }

    /**
     *
     */
    protected function setFieldParams()
    {
        $this->_aFieldParams = [
            '{table_first_field}' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_INT,
                Field::FIELD_PARAM_TYPE_VALUE => 11,
                Field::FIELD_PARAM_OTHER => 'UNSIGNED NOT NULL',
                Field::FIELD_PARAM_PRIMARY_KEY => true,
                Field::FIELD_PARAM_AUTO_INCREMENT => true
            ],
            'table_second_field' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_VARCHAR,
                Field::FIELD_PARAM_TYPE_VALUE => 255
            ],
            'table_third_field' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_VARCHAR,
                Field::FIELD_PARAM_TYPE_VALUE => 255
            ],
            'table_fourth_field' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_VARCHAR,
                Field::FIELD_PARAM_TYPE_VALUE => 32,
                Field::FIELD_PARAM_OTHER => 'DEFAULT \'4.5.0\''
            ],
            'table_fifth_field' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_VARCHAR,
                Field::FIELD_PARAM_TYPE_VALUE => 255
            ],
            //..
        ];
    }
}

 

The location of that file must be: {your_app_path}/Installation/Database/{file_name}.php. Each table is a file.

On file Install.php, add this:

Code Block
languagephp
titleuse
linenumberstrue
$this->database = [            
	'{your_class_name_1}',        
	'{your_class_name_2}',
	//...
;

 

With this way, your database will be automatic create when fresh installation, automatic alter when upgrade, automatic remove when uninstall.