Add a New Table
After creating the project, you're ready to create your own tables.
Create the Table
Create a new migration file in the migrations folder using the format 0000X-message.sql, where X represents a sequential number that determines execution order.
- Create an "up" migration file
db/migrations/up/00002-create-table-example.sql:
create table example_crud
(
id int auto_increment not null primary key,
name varchar(50) not null,
birthdate datetime null,
code int null
);
- Create a corresponding "down" migration file
db/migrations/down/00001-rollback-table-example.sqlfor rollbacks:
drop table example_crud;
Run the Migration
Apply your migrations with:
APP_ENV=dev php gluo migrate:update
# OR
php gluo --env=dev migrate:update
Expected output:
> Environment: dev
> Database: mysql://root:****@127.0.0.1/localdev
Updating to version latest...
Migration completed successfully.
To rollback changes:
APP_ENV=dev php gluo migrate:update --version=1
# OR
php gluo --env=dev migrate:update --version=1
The result should be:
> Environment: dev
> Database: mysql://root:****@127.0.0.1/localdev
Updating to version 1...
Migration completed successfully.
Remember to run the migrating update again to apply the changes.
Generate CRUD Components with the Code Generator
Generate all necessary files for your new table:
# Ensure DB is updated first
APP_ENV=dev php gluo migrate:update
# OR: php gluo --env=dev migrate:update
# Generate files (codegen:all, or one of codegen:model, codegen:repository, codegen:service, codegen:controller, codegen:test)
APP_ENV=dev php gluo codegen:all --table example_crud --save
# OR: php gluo --env=dev codegen:all --table example_crud --save
This creates:
src/Model/ExampleCrud.php- Model classsrc/Repository/ExampleCrudRepository.php- Repository classsrc/Service/ExampleCrudService.php- Service classsrc/Controller/ExampleCrudController.php- REST controllertests/Controller/ExampleCrudTest.php- Functional tests
The repository and service are automatically registered in:
config/dev/04-repositories.phpconfig/dev/05-services.php
No manual configuration needed!
To generate ActiveRecord pattern instead of Repository pattern:
APP_ENV=dev php gluo codegen:all --table example_crud --activerecord --save
# OR: php gluo --env=dev codegen:all --table example_crud --activerecord --save
This generates a simpler architecture with the model containing data access methods. See Code Generator Documentation for details.
Run the Tests
The automatically generated test is located at tests/Controller/ExampleCrudTest.php.
Run it:
php gluo test
Initial tests will fail because we need to:
- Generate OpenAPI documentation to create the endpoints:
php gluo openapi
- Fix the test data by updating
tests/Controller/ExampleCrudTest.php:
Locate:
protected function getSampleData($array = false)
{
$sample = [
'name' => 'name',
'birthdate' => 'birthdate',
'code' => 1,
];
...
And Change:
'birthdate' => 'birthdate',
To:
'birthdate' => '2023-01-01 00:00:00',
- Run the tests again:
php gluo test
Your tests should now pass successfully!
Next Steps
Continue with Adding a New Field to enhance your implementation.