PHP code example of contenir / contenir-db-model

1. Go to this page and download the library: Download contenir/contenir-db-model 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/ */

    

contenir / contenir-db-model example snippets


use Contenir\Db\Model\Collection;
use Contenir\Db\Model\EntityManager;
use Contenir\Db\Model\Mapping\{Column, HasMany, Id, Table, Version};

#[Table('users')]
final class User
{
    #[Id(generated: true)]
    public ?int $id = null;

    #[Column]
    public string $email;

    #[Column('created_at')]
    public DateTimeImmutable $createdAt;

    #[Version]
    public int $version = 1;

    /** @var Collection<Order> */
    #[HasMany(Order::class, foreignKey: 'user_id')]
    public Collection $orders;
}

$em    = new EntityManager($adapter);
$users = $em->getRepository(User::class);

$user        = $users->find(1);
$user->email = '[email protected]';
$em->save($user);   // UPDATE users SET email = ?, version = 2 WHERE id = 1 AND version = 1

foreach ($user->orders as $order) {
    // loaded lazily, in one query
}