This repo is not maintained anymore
For current versions please try the original https://github.com/Laykou/cakephp-schema or a successor of this one: https://github.com/raul338/cakephp-schema, (*1)
Schema plugin for CakePHP 3.0
Save the schema into one file and then restore the database from the schema file. The schema is automatically saved when executing cake migrations migrate
., (*2)
Supported datasources
- Postgres
- MySQL
- SQL Server
- ~~SQLite~~ not yet
Installation
You can install this plugin into your CakePHP application using composer., (*3)
The recommended way to install composer packages is:, (*4)
composer require scherersoftware/cakephp-schema
Update your `config/bootstrap.php``:, (*5)
Plugin::load('Schema', ['bootstrap' => true]);
Usage
The plugin saves the schema of the default
connection to the config/schema.php
file. The structure is similiar to the fixtures fields., (*6)
cake schema save
To load the schema back execute, (*7)
cake schema load
All existing tables in the database are dropped before loading. You will be asked, (*8)
Loading the schema from the file Database is not empty. 37 tables will be deleted.
Do you want to continue? (y/n)
To disable the question (and answer with yes) run, (*9)
cake schema load --no-interaction
Seed
The Schema plugin allows you to seed data from the config/seed.php
file. The seed.php
file should return array of tables and rows:, (*10)
<?php
// You can work with custom libraries here or use the Cake's ORM
return [
'articles' => [
[
'id' => 1,
'category_id' => 1,
'label' => 'CakePHP'
], [
'id' => 2,
'label' => 'Schema plugin',
'json_type_field' => [
'i' => 'will convert',
'to' => 'json'
]
]
],
'categories' => [
[
'id' => 2,
'label' => 'Frameworks'
]
]
];
The Seed commands support the CakePHP ORM's type mapping. So for example, if you're using the JsonType example from the cookbook, the seed commands will automatically convert an array to JSON., (*11)
You can use the schema generateseed
command to automatically generate a seed.php file based on your database contents., (*12)
Use schema seed
for importing the contents of the seed.php
into your DB., (*13)
Seed commands will take the following options:, (*14)
-
connection
Database connection to use.
-
seed
Path to the seed file to generate (Defaults to "config/seed.php")
-
path
Path to the schema.php file (Defaults to "config/schema.php")
Other examples
cake schema save --connection test
cake schema save --path config/schema/schema.lock
cake schema load --connection test --path config/schema/schema.lock --no-interaction
To only drop all tables in database
cake schema drop
cake schema drop --connection test
Seeding Examples
cake schema seed --truncate
cake schema generateseed --seed config/my_seed.php
Seeding for Migrations Plugin / phinx
This plugin provides a bake task extending the seed
bake task provided by the cakephp/migrations
plugin, but with automated inclusion of seed data from the database., (*15)
Example usage:, (*16)
bin/cake bake migration_seed Users --records
This will write a new file into src/config/Seeds/UsersSeed.php
including all records currently present in the DB's users table., (*17)
TODO
- [x] Auto-creation of the schema.php file after
cake migrations migrate
- [x] Data seeding
- [ ] Tests
- [ ] More options and configuration
- [ ] Refactoring and cleaning the code
Known issues
- SQLite is not fully supported