docs section about call with additional arguments

[skip ci]
This commit is contained in:
silverqx
2022-05-17 09:56:41 +02:00
parent f7ecb554a4
commit 153aac62df
4 changed files with 39 additions and 3 deletions

View File

@@ -271,7 +271,7 @@ And paste the following code.
} // namespace Migrations
:::info
If you want, you can also build the `tom` application without the migrations, simply comment out the `migrations()` method and the corresponding `#include "migrations/xyz.hpp"` files.
If you want, you can also build the `tom` application without the migrations, simply comment out the `migrations` method and the corresponding `#include "migrations/xyz.hpp"` files.
:::
### Seeders

View File

@@ -142,7 +142,7 @@ Some database statements do not return any value. For these types of operations,
DB::statement("drop table users");
:::tip
`DB::statement()` should be used for [DDL](https://en.wikipedia.org/wiki/Data_definition_language) queries, don't use it for "select" queries because it internally calls `recordsHaveBeenModified()`.
`DB::statement` method should be used for [DDL](https://en.wikipedia.org/wiki/Data_definition_language) queries, don't use it for "select" queries because it internally calls `recordsHaveBeenModified` method.
:::
#### Running An Unprepared Statement

View File

@@ -17,7 +17,7 @@ The Qt framework version used during development was 5.15.2 and >= 6.2.
Optional:
- [MySQL Connector/C 8](https://dev.mysql.com/downloads/c-api/) - used only for the [`mysql_ping()`](https://dev.mysql.com/doc/c-api/8.0/en/mysql-ping.html) function and provided by [MySQL 8 Server](https://dev.mysql.com/downloads/mysql/)
- [MySQL Connector/C 8](https://dev.mysql.com/downloads/c-api/) - used only for the [`mysql_ping`](https://dev.mysql.com/doc/c-api/8.0/en/mysql-ping.html) function and provided by [MySQL 8 Server](https://dev.mysql.com/downloads/mysql/)
### Install dependencies

View File

@@ -56,6 +56,42 @@ Within the `DatabaseSeeder` class, you may use the `call` method to execute addi
call<UserSeeder, PostSeeder, CommentSeeder>();
}
#### Call with additional arguments
The `call` method allows to pass additional arguments to the seeder/s, but it has additional requirements.
If you define a `run` method without parameters then this method is called using the virtual dispatch (polymorphism) and in this case, you should use the `override` specifier.
If you define your `run` method eg. like this `run(bool shouldSeed)` or whatever parameters you want, then this method is called using the fold expression (virtual dispatch is not used in this case) so you can't use the `override` specifier and you must call the `call<>()` method with exactly the same arguments like the `run` method was defined with, in our example, it should look like this `call<ExampleSeeder>(true)`.
Let's demonstrate it on a small example, following is the `run` method in the root `DatabaseSeeder` class.
/*! Run the database seeders. */
void run() override
{
// This value can be based eg. on data from the database
const auto shouldSeed = true;
call<UserSeeder>(shouldSeed);
}
The `run` method in the `UserSeeder` class.
/*! Run the database seeders. */
void run(const bool shouldSeed)
{
if (!shouldSeed)
return;
DB::table("users")->insert({
{"name", "1. user"},
});
}
:::tip
The `call` method provides two shortcut methods, `callWith` and `callSilent` (no output from seeders).
:::
## Running Seeders
You may execute the `db:seed` tom command to seed your database. By default, the `db:seed` command runs the `Seeders::DatabaseSeeder` class, which may in turn invoke other seed classes. However, you may use the `--class` option to specify a specific seeder class to run individually: