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);
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();
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');