PHP code example of phpaml / data

1. Go to this page and download the library: Download phpaml/data library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpaml / data example snippets


use AML\Data\Connection;
use AML\Data\DbContext;
use AML\Data\DbSet;
use AML\Data\Entity;
use AML\Data\Metadata\{Key, Table};

#[Table('users')]
final class User extends Entity
{
    #[Key]
    public int $id;
    public string $name;
    public string $email;
}

final class AppDbContext extends DbContext
{
    /** @return DbSet<User> */
    public function users(): DbSet
    {
        return $this->set(User::class);
    }
}

$db = new AppDbContext(Connection::sqlite(__DIR__ . '/storage/app.sqlite'));

$user = new User();
$user->name = 'Ada';
$user->email = '[email protected]';
$db->users()->add($user);

$admins = $db->users()
    ->where('email', 'LIKE', '%@example.com')
    ->orderBy('name')
    ->paginate(page: 1, perPage: 20);

return [
    'default' => 'main',
    'connections' => [
        'main' => [
            'driver' => 'sqlite',
            'database' => 'runtime/storage/app.sqlite',
        ],
        'reporting' => [
            'driver' => 'pgsql',
            'host' => '127.0.0.1',
            'port' => 5432,
            'database' => 'reporting',
            'username' => 'phpaml',
            'password' => getenv('REPORTING_PASSWORD'),
        ],
        'documents' => [
            'driver' => 'mongodb',
            'uri' => 'mongodb://127.0.0.1:27017',
            'database' => 'documents',
        ],
    ],
];

$manager = new AML\Data\Connections\ConnectionManager($projectRoot, $config);
$main = $manager->sql();
$reporting = $manager->sql('reporting');
$db = $manager->context(AppDbContext::class, 'main');

use AML\Data\Relations\{BelongsTo, BelongsToMany, HasMany, HasOne};

final class User extends Entity
{
    public int $id;

    /** @var list<Post> */
    #[HasMany(Post::class, foreignKey: 'user_id')]
    public array $posts = [];

    #[HasOne(Profile::class, foreignKey: 'user_id')]
    public ?Profile $profile = null;

    /** @var list<Role> */
    #[BelongsToMany(
        Role::class,
        pivotTable: 'role_user',
        pivotLocalKey: 'user_id',
        pivotTargetKey: 'role_id',
    )]
    public array $roles = [];
}

final class Post extends Entity
{
    public int $id;
    public int $userId;

    #[BelongsTo(User::class, foreignKey: 'user_id')]
    public ?User $author = null;
}

$users = $db->users()->with('posts')->all();

$roles = $db->relation($user, 'roles');
$roles->attach([1, 2]);
$roles->detach(1);
$roles->sync([2, 3]);

use AML\Data\Connection;
use AML\Data\Migrations\Migration;
use AML\Data\Schema\{Schema, Table};

return new class extends Migration {
    public function up(Connection $connection): void
    {
        (new Schema($connection))->create('users', function (Table $table): void {
            $table->id();
            $table->string('name', 120);
            $table->string('email', 180)->unique();
            $table->boolean('active')->default(true);
            $table->text('bio')->nullable();
            $table->timestamps();
        });
    }

    public function down(Connection $connection): void
    {
        (new Schema($connection))->dropIfExists('users');
    }
};

$mysql = new Connection('mysql:host=localhost;dbname=app', 'user', 'password');
$postgres = new Connection('pgsql:host=localhost;dbname=app', 'user', 'password');

$db->add($newUser);
$db->update($existingUser);
$db->remove($obsoleteUser);
$affected = $db->saveChanges();

$db->transaction(function (AppDbContext $db): void {
    $db->users()->add($first);
    $db->users()->add($second);
});
bash
AML_DATA_MYSQL_DSN='mysql:host=127.0.0.1;dbname=phpaml_data_test' \
AML_DATA_MYSQL_USER='phpaml' \
AML_DATA_MYSQL_PASSWORD='secret' \
php tests/databases.php

AML_DATA_PGSQL_DSN='pgsql:host=127.0.0.1;dbname=phpaml_data_test' \
AML_DATA_PGSQL_USER='phpaml' \
AML_DATA_PGSQL_PASSWORD='secret' \
php tests/databases.php
text
src/
  models/
    User.php
  Data/
    AppDbContext.php
database/
  migrations/
  seeders/