PHP code example of flytachi / winter-ppa

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

    

flytachi / winter-ppa example snippets


use Flytachi\Winter\Ppa\Mapping\Attributes\Entity\Table;
use Flytachi\Winter\Ppa\Mapping\Attributes\Hybrid\Id;
use Flytachi\Winter\Ppa\Mapping\Attributes\Primal\Varchar;

#[Table('users')]
class UserEntity
{
    #[Id] public ?int $id = null;
    #[Varchar(255)] public string $email;
    #[Varchar(64)] public string $status;
}

use Flytachi\Winter\Ppa\Stereotype\Repository;

/** @extends Repository<UserEntity> */
class UserRepository extends Repository
{
    public static string $table         = 'users';
    protected string $entityClassName   = UserEntity::class;
    protected string $dbConfigClassName = MainDbConfig::class;
}

use Flytachi\Winter\Cdo\Qb;

$user  = UserRepository::instance()->findById(42);
$users = UserRepository::instance('u')
    ->where(Qb::eq('u.status', 'active'))
    ->orderBy('u.created_at DESC')
    ->limit(20)
    ->findAll();

$id = UserRepository::instance()->insert(['email' => $email, 'status' => 'active']);

// reading
$repo->find();                      $repo->findAll();
$repo->findById($id);               $repo->findBy(Qb::eq('email', $email));
$repo->count();                     $repo->exists();
$repo->findByIdOrThrow($id);        // instead of the null check

// writing
$repo->insert($entity);             $repo->insertBatch(...$entities);
$repo->update($entity, Qb::eq('id', $id));
$repo->delete(Qb::lt('created_at', $cutoff));
$repo->upsert($entity, ['email']);  $repo->upsertBatch(['email'], ...$entities);

// assembling
UserRepository::instance('u')
    ->select(['u.id', 'COUNT(o.id) AS orders'])
    ->joinLeft(OrderRepository::instance('o'), 'o.user_id = u.id')
    ->where(Qb::eq('u.status', 'active'))
    ->groupBy('u.id')
    ->having('COUNT(o.id) > 3')
    ->findAll();

// inspecting, before anything reaches the database
$repo->buildSql();                  $repo->getSql('binds');

use Flytachi\Winter\Ppa\Pool\PoolTelemetry;
use Flytachi\Winter\Ppa\Pool\PpaConnectionPool;

PpaConnectionPool::setLogger($logger);                       // unset: silent
PpaConnectionPool::setTimezoneProvider(fn() => $zone);       // unset: no SET TIMEZONE at all
PoolTelemetry::setStoreProvider(fn() => $storage);           // unset: nothing is published