-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Install Atlas with Composer:
composer require rebelcode/atlas
Start by creating an Atlas object.
use RebelCode\Atlas\Atlas;
$atlas = Atlas::createDefault();For now, we'll use the default configuration. If you need to configure Atlas to generate queries in different ways, refer to the Configuration page.
Now that we have our Atlas object, we can obtain table objects:
$users = $atlas->table('my_table');The table objects provide access to the majority of Atlas' API. They are used to create queries, and initiate expression building in WHERE conditions and JOIN search conditions.
If you need to generate queries that create the table, you'll also need to specify a Schema. Let's create a simple a users table:
use RebelCode\Atlas\Schema;
use RebelCode\Atlas\Schema\Column;
use RebelCode\Atlas\Schema\PrimaryKey;
$users = $atlas->table('users', new Schema(
// COLUMNS
[
'id' => Column::ofType('BIGINT(20) UNSIGNED')->autoInc(),
'name' => Column:ofType('VARCHAR(100)'),
'email' => Column:ofType('VARCHAR(200)'),
'points' => Column:ofType('INT UNSIGNED')->nullable()->default(0),
],
// KEYS
[
'users_pk' => new PrimaryKey(['id']),
],
));Now we can generate our CREATE TABLE and CREATE INDEX queries, then send the queries to our database using your preferred MySQL client. In these examples, we'll be using the mysqli PHP extension.
$queries = $users->create();
foreach ($queries as $query) {
$mysqli->query($query);
}Now that our table has been created, let's add some records to it:
$mysli->query(
$users->insert([
[
'name' => 'Adam',
'email' => 'adam@example.com',
'points' => 12,
],
[
'name' => 'Bob',
'email' => 'bob@example.com',
'points' => 15,
],
[
'name' => 'Charles',
'email' => 'charles@example.com',
'points' => 4,
]
])
);Finally, let's query the table for all users that have 10 points or more:
$mysli->query(
$users->select(['*'])
->where($users->column('points')->gte(10))
);