Page tree

Versions Compared

Key

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

We provide 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:

Requires: phpFox 4.5+

...

In this tutorial, we will use the source of the app To Do List as en example:

  • To adding new table, you need define a class for it:
Code Block
languagephp
titlethemetable.phpRDark
borderStylesolid
linenumberstrue
collapsetrue

<?php

namespace Apps\{your_app_id}TodoList\Installation\Database;

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

//class nameTodoTaskTable mustextends beTable
the{
same with file name class {your_class_name} extends Table
{
    /**
     */**
     * Set name of this table, can't missing
       */
    protected function setTableName()
    {
        $this->_table_name = '{your_table_name}todolist_task';
    }

    /**
     * Set all fields of table
     */
    protected function setFieldParams()
    {
        $this->_aFieldParams = [
            '{table_first_field}task_id' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_INT'primary_key' => true,
                'auto_increment' => true,
                'type' => 'int',
                'type_value' => 10,
                Field::FIELD_PARAM_TYPE_VALUE => 11'other' => 'UNSIGNED NOT NULL'
            ],
            'user_id' => [
                'type' => 'int',
                Field::FIELD_PARAM_OTHER'type_value' => 10,
                'other' => 'UNSIGNED NOT NULL'
            ],
            'name' =>   Field::FIELD_PARAM_PRIMARY_KEY => true[
                'type' => 'varchar',
                'type_value' => 255,
                'other' => 'NOT NULL DEFAULT \'\''
            ],
            'description' => [
                'type' => 'text',
                'other' => 'NOT NULL DEFAULT \'\''
            ],
            'task_status' => [
                'type' => 'tinyint',
                Field::FIELD_PARAM_AUTO_INCREMENT'type_value' => true3,
                'other' => 'UNSIGNED NOT NULL DEFAULT 0'
            ],
            'tableis_second_fieldsponsor' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_VARCHAR'type' => 'tinyint',
                'type_value' => 1,
                'other' => 'UNSIGNED NOT NULL DEFAULT 0'
            ],
            'time_stamp' => [
                'type' => 'int',
                Field::FIELD_PARAM_TYPE_VALUE'type_value' => 25510,
                'other' => 'UNSIGNED NOT NULL'
            ],
            'table_third_field' => time_update' => [
                'type' => 'int',
                'type_value' => 10,
                'other' => 'UNSIGNED NOT NULL DEFAULT 0'
            ],
            'privacy' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_VARCHAR'type' => 'tinyint',
                'type_value' => 1,
                'other' => 'NOT NULL DEFAULT 0'
            ],
            'total_comment' => [
                'type' => 'int',
                Field::FIELD_PARAM_TYPE_VALUE'type_value' => 25510,
                'other' => 'UNSIGNED NOT NULL DEFAULT 0'
            ],
            'tabletotal_fourth_fieldview' => [
                Field::FIELD_PARAM_TYPE'type' => 'int',
                'type_value' => Field::TYPE_VARCHAR10,
                Field::FIELD_PARAM_TYPE_VALUE => 32'other' => 'UNSIGNED NOT NULL DEFAULT 0'
            ],
            'total_like' => [
                'type' => 'int',
                Field::FIELD_PARAM_OTHER'type_value' => 10,
                'other' => 'UNSIGNED NOT NULL DEFAULT \'4.5.0\''
            ],
            'tableimage_fifth_fieldpath' => [
                Field::FIELD_PARAM_TYPE => Field::TYPE_VARCHAR'type' => 'varchar',
                'type_value' => '75',
                'other' => 'DEFAULT NULL',
            ],
            'server_id' => [
                'type' => 'tinyint',
                Field::FIELD_PARAM_TYPE_VALUE'type_value' => 255'1',
            ]    'other' => 'NOT NULL DEFAULT \'0\'',
            //..],
        ];
    }
}

 

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

...

    /**
     * Set keys of table
     */
    protected function setKeys()
    {
        $this->_key = [
            'task_id' => ['task_id','task_status'],
            'user_id' => ['user_id']
        ];
    }
}

Final, in file Install.php (configuration file of your app), add the table to your app database configuration:

$this->database = [
Code Block
languagephp
titlethemeuseRDark
borderStylesolid
linenumberstrue
collapsefalse

protected function setOthers()
{
    ...
    
	'{your_class_name_1}',   // list all table class
    
	'{your_class_name_2}',
	//$this->database = ['TodoTaskTable'];
    ...
;

 

...

}

Congratulation! You have just added a table for your app successfully! The table will be created/altered automatically when admin install/upgrade the app. The Core will take care it for you.