To make use of databases in your application, configure your database details in the db config file. The db config file is located in project_dir/config/db.php. You can manually create the file if missing.
config/db.php
<?php
/**
* Database configurations
* */
use App\Databases\Schemas\{
DatabaseSchema,
AnotherDatabaseSchema
};
use App\Databases\Seeders\DatabaseSeeder;
return [
'default_connection' => 'main',
'default_database' => 'your-db-name',
'connections' => [
'main' => [
'driver' => 'mysql',
'port' => 3306,
'username' => env('db_user_name', ''),
'password' => env('db_password', ''),
'host' => env('db_host', 'localhost'),
//...other connection properties
'databases' => [
'your-db-name' => DatabaseSchema::class,
'another-db-name' => AnotherDatabaseSchema::class
]
],
],
'seeder' => DatabaseSeeder::class,
]
?>
In SaQle, a database connection is simply the server that runs your relational database.
An application may require connection to multiple database servers. A server may run multiple databases.
The db configuration file is designed to reflect this arrangment. Here are the config settings you need to have
When your application is set to connect to multiple database servers, the default connection key specifies which connection to use by default.
If not provided, the first connection in the list of connections will be used as the default connection
When your default connection has multiple databases, the default database key specifies which database of the default connection to use by default.
If not provided, the first database in the list of databases will be used as the default database
Note: each database listed is accompanied by a database schema class. See schemas
This is a list of databases servers and the databases that are available for your application
The seeder key specifies a database seeder class. This class is used by the framework to fill your db with initial data.
See seeders
SaQle models are designed to be primarily domain objects. To create relationships between database and database tables to models, you define a database schema class in project_dir/databases/schemas. This class must extend SaQle\Orm\Database\Schema and define a protected array property $models.
The property $models is an associative array, where the keys are table names, and the values are the model classes to associate to those tables
DatabaseSchema.php
<?php
declare(strict_types = 1);
namespace App\Databases\Schemas;
use SaQle\Orm\Database\Schema;
use App\Modules\Inventory\Models\{
Product,
Order
};
final class InventorySchema extends Schema {
protected array $models = [
'products' => Product::class,
'orders' => Order::class,
//list other inventory related models here
];
}
Migrations provide means for the application to keep track of all the changes in your models, hence your database and tables, as they happen.
To generate migration files when your models change, run php saqle make:migrations -n=MigrationName
To effect migrations on actual database, run php saqle migrate
Generated migration files are saved here: project_dir/databases/migrations
Snapshots are generated every time you run php saqle make:migrations -n=MigrationName alongside the migration files.
Snapshots keep a record of the whole database structure at the time you are making migrations
Generated snapshot files are saved here: project_dir/databases/snapshots
You define the data to seed your database with in seeder files. A seeder file is a php file that returns an array of data to seed.
Seeder files are defined in: project_dir/databases/seeders/data
products.php
<?php
return [
[
"code" => "SAM-A16-128-BLK",
"name" => "Samsung Galaxy A16 5G",
"model" => "Galaxy A16 5G",
"color" => "black",
"storage" => "128GB",
"ram" => "8GB"
],
[
"code" => "TEC-SP20-256-BLK",
"name" => "Tecno Spark 20 Pro+",
"model" => "Spark 20 Pro+",
"color" => "black",
"storage" => "256GB",
"ram" => "8GB"
],
[
"code" => "INF-N40P-256-GLD",
"name" => "Infinix Note 40 Pro",
"model" => "Note 40 Pro",
"color" => "gold",
"storage" => "256GB",
"ram" => "8GB"
],
];
?>
A seeder class provides the relationship between the model and the seeder file. This class must extend SaQle\Core\Migration\Seed\DbSeeder and define a public static function get_seeds() method that returns an associative array.
Seeder classes are defined in: project_dir/databases/seeders
InventorySeeder.php
<?php
namespace App\Databases\Seeders;
use SaQle\Core\Migration\Seed\DbSeeder;
use App\Modules\Inventory\Models\Product;
class InventorySeeder extends DbSeeder {
public static function get_seeds() : array {
return [
Product::class => "data/products.php",
//other seeding entries here
];
}
}
?>
Seed your database by running: php saqle db:seed
NOTE: Do not seed data that needs to go through your business logic. Seeded data will be inserted directly into your tables and will bypass any business logic.