1. Go to this page and download the library: Download denisyu-1/articulate 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/ */
denisyu-1 / articulate example snippets
use Articulate\Connection;
use Articulate\Modules\EntityManager\EntityManager;
#[Entity]
class User
{
#[PrimaryKey]
public ?int $id = null;
#[Property]
public string $name;
#[Property]
public string $email;
}
$connection = new Connection('mysql:host=127.0.0.1;dbname=myapp', 'user', 'password');
$em = new EntityManager($connection);
$user = new User();
$user->name = 'Jane';
$user->email = '[email protected]';
$em->persist($user);
$em->flush();
$user = $em->getRepository(User::class)->find($user->id);
#[Entity]
class User
{
public int $id;
public string $login;
public string $password;
public string $name;
public array $phones; // Auth doesn't need this
public array $groups; // Auth doesn't need this
public Cart $cart; // Auth doesn't need this
}
// Auth: loads full user + all relations
$user = $userRepo->find($id);
return $auth->validate($user->login, $user->password);
#[Entity(tableName: 'user')]
class LoginUser
{
#[PrimaryKey]
public int $id;
#[Property]
public string $login;
#[Property]
public string $password;
}
#[Entity]
class User
{
#[PrimaryKey]
public int $id;
#[Property]
public string $name;
#[OneToMany(ownedBy: 'user', targetEntity: Phone::class)]
public array $phones;
#[OneToOne(targetEntity: Cart::class, referencedBy: 'user')]
public Cart $cart;
}
// Auth: loads only id, login, password
$loginUser = $em->getRepository(LoginUser::class)->find($id);
return $auth->validate($loginUser->login, $loginUser->password);
#[Entity(tableName: 'user', readOnly: true)]
class LoginUser
{
#[PrimaryKey]
public int $id;
#[Property]
public string $login;
#[Property]
public string $password;
}
// find() and QueryBuilder work normally:
$loginUser = $em->getRepository(LoginUser::class)->find($id);
$auth->validate($loginUser->login, $loginUser->password);
// persist() and remove() throw ReadOnlyEntityException:
$em->persist($loginUser); // throws
use Articulate\Attributes\Relations\MorphedByMany;
use Articulate\Attributes\Relations\MorphToMany;
use Articulate\Attributes\Relations\MorphTypeRegistry;
use Articulate\Modules\EntityManager\Collection;
MorphTypeRegistry::register(TaggableOrder::class, 'order');
MorphTypeRegistry::register(TaggableCustomer::class, 'customer');
#[Entity(tableName: 'orders')]
class TaggableOrder
{
#[PrimaryKey]
public int $id;
#[MorphToMany(targetEntity: Tag::class, name: 'taggable', targetIdColumn: 'tag_id')]
public Collection $tags;
}
#[Entity(tableName: 'tags')]
class Tag
{
#[PrimaryKey]
public int $id;
#[MorphedByMany(targetEntity: TaggableOrder::class, name: 'taggable', targetIdColumn: 'tag_id')]
public array $orders;
}
$order->tags->add($tag);
$em->flush(); // inserts into taggables using taggable_type = 'order'