From 153aac62df8031f05a3b9a81044bfdd9ead2485f Mon Sep 17 00:00:00 2001 From: silverqx Date: Tue, 17 May 2022 09:56:41 +0200 Subject: [PATCH] docs section about call with additional arguments [skip ci] --- docs/building-migrations.mdx | 2 +- docs/database.mdx | 2 +- docs/dependencies.mdx | 2 +- docs/seeding.mdx | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/docs/building-migrations.mdx b/docs/building-migrations.mdx index 1cb937340..ac19bd728 100644 --- a/docs/building-migrations.mdx +++ b/docs/building-migrations.mdx @@ -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 diff --git a/docs/database.mdx b/docs/database.mdx index 8ae0945d1..140f17c2b 100644 --- a/docs/database.mdx +++ b/docs/database.mdx @@ -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 diff --git a/docs/dependencies.mdx b/docs/dependencies.mdx index 30fad7a89..60e1be5b0 100644 --- a/docs/dependencies.mdx +++ b/docs/dependencies.mdx @@ -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 diff --git a/docs/seeding.mdx b/docs/seeding.mdx index 7d826e56c..378147b24 100644 --- a/docs/seeding.mdx +++ b/docs/seeding.mdx @@ -56,6 +56,42 @@ Within the `DatabaseSeeder` class, you may use the `call` method to execute addi call(); } +#### 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(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(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: