Download the PHP package kristijorgji/db-to-php without Composer
On this page you can find all versions of the php package kristijorgji/db-to-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package db-to-php
db-to-php
A framework for automating PHP code generation for working with databases.
Many times we need data object classes corresponding to different database tables. For testing and populating the databases with dummy data we also require factories which generate dummy/fake data for those tables.
A lot of time is spent doing those tasks manually, so I decided to automate the process. Not only entities and factories are created by this library, but the original type is also detected and used in the classes and in the factory values generator.
For now the library supports only MySql as a database driver.
- Create entity classes based on your database tables.
- Create factories for automatic generation of entities with random data, or just random data for tables
- (Coming soon) Create repositories for writing/reading these entities
- (Coming soon) Working to support different database drivers aside from MySql
Table of Contents
- Installation
- Config
- Usage
- Generate Entities
- Instructions
- Example execution
- Generate Factories
- Instructions
- Example execution
- Generate Entities
- License
Installation
Run the following command to initialize dbToPhp
This command will create in your project root folder the config file dbToPhp.cfg.php
You need to edit that to your desired settings.
Config
The config and it's keys try to be as much self explanatory as possible. Example config:
You must setup the proper database credentials and driver (for now only MySql is supported). The
- if set to true the generated PHP code will use typehints in the function parameters and also as return tyeps (PHP 7 /7.1)
Example with :
Example with :
Usage
Generate Entities
Features
- Detect database field type and use corresponding php type for the property
- Detect if a database field is nullable and generate corresponding properties and methods
- (Optional) You can choose custom entity class names for specific tables
- You can generate entities for all the tables or only for those that you specify
- You can use annotations (PHPDoc) or not depending on the configuration that you setup
- You can configure the access modifier of the entity properties
- You can configure if you want to generate setter methods or not and if you want them to be fluent
- You can configure if you want to generate getter methods
- You can configure if you want to generate type hinted getters/setters
Generate Entities Instructions
First make sure to have setup correctly your database connection and credentials
in the config file that is generated after the installation dbToPhp.cfg.php
.
If you want the result code to be generated for php that supports typehinting and return types (also nullable like ?string) set typeHint key to true.
The other options are self explanatory. Change anything you like under the entities key in the config.
Below is shown only the part of the config for the entities generation, you can see a full configuration example in the config section.
Then just run from your terminal
Example entity generation
In my demo setup I only had selected the table below with the following MySql schema:
The generated entity class is given below (please note that class name, namespace, usage of annotations, typehinting, setters/getters etc anything can be customized in the configuration file):
https://github.com/kristijorgji/db-to-php/blob/master/docs/samples/entities/UsersDemoEntity.php
The name for the entity is UsersDemoEntity, but that can be customized and set in the config section 'entities'['tableToEntityClassName']
Generate Factories
Features:
- Detect enums and generate only the allowed values
- Detect if the integers are signed or not and their ranges
- Detect the precision of the float/double/decimals and generate values up to that max precision
- Detect json fields and generates valid json
- Detects date, year and generates proper values
- Detects if the field is nullable
(See example execution for demonstration)
Generate Factories Instructions
The config section for the factories is similar and self explanatory like the one for the entities:
After configuring the section to your preferences, execute the following to generate the factories:
Example factory generation
I am using as an example the same table that I used for generating entities.
The result factory is given below. As you can notice every data generated is detailed according to the table specifications.
License
db-to-php is released under the MIT Licence. See the bundled LICENSE file for details.