Purpose of this document is provide you the guideline how to add database tables for your apps. This new approach is in process to apply for all core apps.Requires: phpFox 4.5+
In this tutorial, we will use the source of the app To Do List as en example:
<?php namespace Apps\TodoList\Installation\Database; use Core\App\Install\Database\Table; class TodoTaskTable extends Table { /** * Set name of this table, can't missing */ protected function setTableName() { $this->_table_name = 'todolist_task'; } /** * Set all fields of table */ protected function setFieldParams() { $this->_aFieldParams = [ 'task_id' => [ 'primary_key' => true, 'auto_increment' => true, 'type' => 'int', 'type_value' => 10, 'other' => 'UNSIGNED NOT NULL' ], 'user_id' => [ 'type' => 'int', 'type_value' => 10, 'other' => 'UNSIGNED NOT NULL' ], 'name' => [ 'type' => 'varchar', 'type_value' => 255, 'other' => 'NOT NULL DEFAULT \'\'' ], 'description' => [ 'type' => 'text', 'other' => 'NOT NULL DEFAULT \'\'' ], 'task_status' => [ 'type' => 'tinyint', 'type_value' => 3, 'other' => 'UNSIGNED NOT NULL DEFAULT 0' ], 'is_sponsor' => [ 'type' => 'tinyint', 'type_value' => 1, 'other' => 'UNSIGNED NOT NULL DEFAULT 0' ], 'time_stamp' => [ 'type' => 'int', 'type_value' => 10, 'other' => 'UNSIGNED NOT NULL' ], 'time_update' => [ 'type' => 'int', 'type_value' => 10, 'other' => 'UNSIGNED NOT NULL DEFAULT 0' ], 'privacy' => [ 'type' => 'tinyint', 'type_value' => 1, 'other' => 'NOT NULL DEFAULT 0' ], 'total_comment' => [ 'type' => 'int', 'type_value' => 10, 'other' => 'UNSIGNED NOT NULL DEFAULT 0' ], 'total_view' => [ 'type' => 'int', 'type_value' => 10, 'other' => 'UNSIGNED NOT NULL DEFAULT 0' ], 'total_like' => [ 'type' => 'int', 'type_value' => 10, 'other' => 'UNSIGNED NOT NULL DEFAULT 0' ], 'image_path' => [ 'type' => 'varchar', 'type_value' => '75', 'other' => 'DEFAULT NULL', ], 'server_id' => [ 'type' => 'tinyint', 'type_value' => '1', 'other' => 'NOT NULL DEFAULT \'0\'', ], ]; } /** * 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:
protected function setOthers() { ... // list all table class $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.