Skip to content

docs: improve seeds.rst #7367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions user_guide_src/source/dbmgmt/seeds.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,57 @@ Database Seeding

Database seeding is a simple way to add data into your database. It is especially useful during development where
you need to populate the database with sample data that you can develop against, but it is not limited to that.
Seeds can contain static data that you don't want to include in a migration, like countries, or geo-coding tables,
Seeders can contain static data that you don't want to include in a migration, like countries, or geo-coding tables,
event or setting information, and more.

Database seeds are simple classes that must have a **run()** method, and extend ``CodeIgniter\Database\Seeder``.
Within the **run()** the class can create any form of data that it needs to. It has access to the database
.. contents::
:local:
:depth: 2

****************
Database Seeders
****************

Database seeders are simple classes that must have a ``run()`` method, and extend ``CodeIgniter\Database\Seeder``.
Within the ``run()`` the class can create any form of data that it needs to. It has access to the database
connection and the forge through ``$this->db`` and ``$this->forge``, respectively. Seed files must be
stored within the **app/Database/Seeds** directory. The name of the file must match the name of the class.

.. literalinclude:: seeds/001.php

***************
Nesting Seeders
===============
***************

Seeders can call other seeders, with the **call()** method. This allows you to easily organize a central seeder,
Seeders can call other seeders, with the ``call()`` method. This allows you to easily organize a central seeder,
but organize the tasks into separate seeder files:

.. literalinclude:: seeds/002.php

You can also use a fully-qualified class name in the **call()** method, allowing you to keep your seeders
You can also use a fully-qualified class name in the ``call()`` method, allowing you to keep your seeders
anywhere the autoloader can find them. This is great for more modular code bases:

.. literalinclude:: seeds/003.php

*************
Using Seeders
=============
*************

You can grab a copy of the main seeder through the database config class:

.. literalinclude:: seeds/004.php

Command Line Seeding
--------------------
====================

You can also seed data from the command line, as part of the Migrations CLI tools, if you don't want to create
a dedicated controller::

> php spark db:seed TestSeeder

Creating Seed Files
-------------------
*********************
Creating Seeder Files
*********************

Using the command line, you can easily generate seed files.

Expand All @@ -52,10 +63,14 @@ Using the command line, you can easily generate seed files.
> php spark make:seeder user --suffix
// Output: UserSeeder.php file located at app/Database/Seeds directory.

You can supply the **root** namespace where the seed file will be stored by supplying the ``--namespace`` option::
You can supply the ``root`` namespace where the seed file will be stored by supplying the ``--namespace`` option::

For Unix:
> php spark make:seeder MySeeder --namespace Acme\\Blog

If ``Acme\Blog`` is mapped to ``app/Blog`` directory, then this command will generate ``MySeeder.php`` at ``app/Blog/Database/Seeds`` directory.
For Windows:
> php spark make:seeder MySeeder --namespace Acme\Blog

If ``Acme\Blog`` is mapped to **app/Blog** directory, then this command will generate **MySeeder.php** at **app/Blog/Database/Seeds** directory.

Supplying the ``--force`` option will overwrite existing files in destination.