Code generation is on of key features of the Composite Sync package. This enables you to generate Entity classes directly from SQL tables, thereby enabling a literal reflection of the SQL table schema into native PHP classes.
- MySQL
- Postgres
- SQLite
To begin using Composite Sync in your project, follow these steps:
$ composer require compositephp/sync
You need to configure ConnectionManager, see instructions here
Add symfony/console commands to your application:
- Composite\Sync\Commands\MigrateCommand
- Composite\Sync\Commands\MigrateNewCommand
- Composite\Sync\Commands\MigrateDownCommand
Here is an example of a minimalist, functional PHP file if you don't have configured symfony/console:
<?php declare(strict_types=1);
include 'vendor/autoload.php';
use Composite\Sync\Commands;
use Symfony\Component\Console\Application;
//may be changed with .env file
putenv('CONNECTIONS_CONFIG_FILE=/path/to/your/connections/config.php');
$app = new Application();
$app->addCommands([
new Commands\GenerateEntityCommand(),
new Commands\GenerateTableCommand(),
]);
$app->run();
The command examines the specific SQL table and generates an Composite\Entity\AbstractEntity PHP class. This class embodies the table structure using native PHP syntax, thereby representing the original SQL table in a more PHP-friendly format.
php cli.php composite:generate-entity connection_name TableName 'App\Models\EntityName'
Argument | Required | Description |
---|---|---|
connection | Yes | Name of connection from connection config file |
table | Yes | Name of SQL table |
entity | Yes | Full classname of the class that needs to be created |
Options:
Option | Description |
---|---|
--force | Overwrite existing file |
The command examines the specific Entity and generates a Table PHP class. This class acts as a gateway to a specific SQL table, providing user-friendly CRUD tools for interacting with SQL right off the bat.
php cli.php composite:generate-table connection_name TableName 'App\Models\EntityName'
Argument | Required | Description |
---|---|---|
entity | Yes | Full Entity classname |
table | No | Full Table classname that needs to be created |
Options:
Option | Description |
---|---|
--cached | Generate cached version of PHP Table class |
--force | Overwrite existing file |