/var/www/vhosts/ihelp.ro/httpdocs/vendor/robmorgan/phinx/docs/en
Edit: /var/www/vhosts/ihelp.ro/httpdocs/vendor/robmorgan/phinx/docs/en/seeding.rst (6451B)
.. index::
single: Database Seeding
Database Seeding
================
In version 0.5.0 Phinx introduced support for seeding your database with test
data. Seed classes are a great way to easily fill your database with data after
it's created. By default they are stored in the `seeds` directory; however, this
path can be changed in your configuration file.
.. note::
Database seeding is entirely optional, and Phinx does not create a `seeds`
directory by default.
Creating a New Seed Class
-------------------------
Phinx includes a command to easily generate a new seed class:
.. code-block:: bash
$ php vendor/bin/phinx seed:create UserSeeder
If you have specified multiple seed paths, you will be asked to select which
path to create the new seed class in.
It is based on a skeleton template:
.. code-block:: php
'foo',
'created' => date('Y-m-d H:i:s'),
],[
'body' => 'bar',
'created' => date('Y-m-d H:i:s'),
]
];
$posts = $this->table('posts');
$posts->insert($data)
->saveData();
}
}
.. note::
You must call the `saveData()` method to commit your data to the table. Phinx
will buffer data until you do so.
Truncating Tables
-----------------
In addition to inserting data Phinx makes it trivial to empty your tables using the
SQL `TRUNCATE` command:
.. code-block:: php
'foo',
'created' => date('Y-m-d H:i:s'),
],
[
'body' => 'bar',
'created' => date('Y-m-d H:i:s'),
]
];
$posts = $this->table('posts');
$posts->insert($data)
->saveData();
// empty the table
$posts->truncate();
}
}
.. note::
SQLite doesn't natively support the `TRUNCATE` command so behind the scenes
`DELETE FROM` is used. It is recommended to call the `VACUUM` command
after truncating a table. Phinx does not do this automatically.
Executing Seed Classes
----------------------
This is the easy part. To seed your database, simply use the `seed:run` command:
.. code-block:: bash
$ php vendor/bin/phinx seed:run
By default, Phinx will execute all available seed classes. If you would like to
run a specific class, simply pass in the name of it using the `-s` parameter:
.. code-block:: bash
$ php vendor/bin/phinx seed:run -s UserSeeder
You can also run multiple seeders:
.. code-block:: bash
$ php vendor/bin/phinx seed:run -s UserSeeder -s PermissionSeeder -s LogSeeder
You can also use the `-v` parameter for more output verbosity:
.. code-block:: bash
$ php vendor/bin/phinx seed:run -v
The Phinx seed functionality provides a simple mechanism to easily and repeatably
insert test data into your database.