PHP code example of sympress / orm

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

    

sympress / orm example snippets




declare(strict_types=1);

namespace App\Mailer\Entity;

use App\Mailer\Repository\EmailLogRepository;
use SymPress\Orm\Mapping\Column;
use SymPress\Orm\Mapping\Entity;
use SymPress\Orm\Mapping\Id;
use SymPress\Orm\Mapping\Index;

#[Entity(table: 'sympress_mailer_logs', repositoryClass: EmailLogRepository::class)]
#[Index(name: 'status_created', columns: ['status', 'createdAt'])]
final class EmailLog
{
    public function __construct(
        #[Id]
        #[Column(type: 'string', length: 32)]
        public string $id,

        #[Column(type: 'datetime')]
        public \DateTimeImmutable $createdAt,

        #[Column(type: 'string', length: 20)]
        public string $status,

        #[Column(type: 'json', nullable: true)]
        public array $payload = [],
    ) {
    }
}

use App\Mailer\Entity\EmailLog;
use SymPress\Orm\EntityManager;

final readonly class MailerService
{
    public function __construct(private EntityManager $entities)
    {
    }

    public function logQueuedMail(string $id, array $payload): void
    {
        $log = new EmailLog(
            $id,
            new \DateTimeImmutable(),
            'queued',
            $payload,
        );

        $this->entities->persist($log);
        $this->entities->flush();
    }
}

$log = $entityManager->find(EmailLog::class, 'log_123');

if ($log === null) {
    return;
}

$log->status = 'sent';

$entityManager->flush();

$log = $entityManager->find(EmailLog::class, 'log_123');

if ($log !== null) {
    $entityManager->remove($log);
    $entityManager->flush();
}

$first = $entityManager->find(EmailLog::class, 'log_123');
$second = $entityManager->find(EmailLog::class, 'log_123');

assert($first === $second);

$entityManager->detach($log);
$entityManager->clear();



declare(strict_types=1);

namespace App\Mailer\Repository;

use App\Mailer\Entity\EmailLog;
use SymPress\Orm\Repository;

final class EmailLogRepository extends Repository
{
    /** @return list<EmailLog> */
    public function queued(int $limit = 50): array
    {
        return $this->findBy(
            ['status' => 'queued'],
            ['createdAt' => 'ASC'],
            $limit,
        );
    }
}

$logs = $entityManager->getRepository(EmailLog::class);

$one = $logs->find('log_123');
$all = $logs->findAll();
$queued = $logs->findBy(['status' => 'queued']);
$latest = $logs->findOneBy(['status' => 'sent']);

$logs->save($log);
$logs->remove($log);
$entityManager->flush();

$logs->save($log, flush: true);
$logs->remove($log, flush: true);

$query = $entityManager
    ->createQueryBuilder()
    ->select('l')
    ->from(EmailLog::class, 'l')
    ->where('l.status = :status')
    ->setParameter('status', 'queued')
    ->orderBy('l.createdAt', 'ASC')
    ->setMaxResults(50)
    ->getQuery();

$logs = $query->getResult();

$query = $entityManager->createQuery(
    'SELECT l FROM EmailLog l WHERE l.status = :status ORDER BY l.createdAt DESC',
    ['status' => 'queued'],
);

$logs = $query->getResult();

$entityManager
    ->createQuery(
        'UPDATE EmailLog l SET l.status = :status WHERE l.id = ?1',
        ['status' => 'sent', 1 => 'log_123'],
    )
    ->execute();

$entityManager->transactional(function (EntityManager $entities) use ($log): void {
    $entities->persist($log);

    $log->status = 'sent';
});

$sql = $schemaTool->getUpdateSchemaSql('sympress-mailer-pro');