PHP code example of precision-soft / doctrine-utility
1. Go to this page and download the library: Download precision-soft/doctrine-utility 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/ */
precision-soft / doctrine-utility example snippets
declare(strict_types=1);
namespace Acme\Domain\Product\Entity;
use Doctrine\ORM\Mapping as ORM;
use PrecisionSoft\Doctrine\Utility\Entity\CreatedTrait;
use PrecisionSoft\Doctrine\Utility\Entity\ModifiedTrait;
use PrecisionSoft\Doctrine\Utility\Repository\DoctrineRepository;
#[ORM\Entity(repositoryClass: DoctrineRepository::class)]
#[ORM\ChangeTrackingPolicy('DEFERRED_EXPLICIT')]
#[ORM\Table(options: ['collate' => 'utf8mb4_general_ci'])]
class Product
{
use CreatedTrait;
use ModifiedTrait;
#[ORM\Id]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 64, nullable: false, unique: true)]
private string $barcode;
#[ORM\ManyToOne(targetEntity: ProductType::class, fetch: 'EXTRA_LAZY')]
#[ORM\JoinColumn(nullable: false, onDelete: 'RESTRICT')]
private ProductType $productType;
}
declare(strict_types=1);
namespace Acme\Domain\Product\Repository;
use Acme\Domain\Product\Entity\Product;
use Acme\Domain\Product\Exception\Exception;
use Acme\Domain\Product\Exception\NotFoundException;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use PrecisionSoft\Doctrine\Utility\Join\JoinCollection;
use PrecisionSoft\Doctrine\Utility\Repository\AbstractRepository;
class ProductRepository extends AbstractRepository
{
public const JOIN_PRODUCT_TYPE = 'joinProductType';
protected function getEntityClass(): string
{
return Product::class;
}
public function find(int $productId): Product
{
/** @var Product|null $product */
$product = $this->getDoctrineRepository()->find($productId);
if (null === $product) {
throw new NotFoundException('the product was not found');
}
return $product;
}
protected function attachCustomFilters(QueryBuilder $queryBuilder, array $filters): JoinCollection
{
$joins = new JoinCollection();
foreach ($filters as $key => $value) {
switch ($key) {
case 'barcodeLike':
$baseKey = \substr($key, 0, -4);
$queryBuilder
->andWhere(static::getAlias() . ".{$baseKey} LIKE :{$key}")
->setParameter($key, $value);
break;
case static::JOIN_PRODUCT_TYPE:
$joins->addJoin(
new Join(
$value,
static::getAlias() . '.productType',
ProductTypeRepository::getAlias(),
),
);
break;
default:
throw new Exception(\sprintf('invalid filter `%s` for `%s::%s`', $key, static::class, __FUNCTION__));
}
}
return $joins;
}
}
use PrecisionSoft\Doctrine\Utility\Repository\AbstractRepository;
use PrecisionSoft\Doctrine\Utility\Repository\EmptyArrayFilterBehavior;
class ProductRepository extends AbstractRepository
{
protected function getFlags(): array
{
return [
EmptyArrayFilterBehavior::class => EmptyArrayFilterBehavior::ThrowException,
];
}
}
use Psr\Log\LoggerInterface;
class ProductRepository extends AbstractRepository
{
public function __construct(private LoggerInterface $logger) {}
protected function getLogger(): ?LoggerInterface
{
return $this->logger;
}
}
use PrecisionSoft\Doctrine\Utility\Service\MysqlLockService;
public function __construct(private MysqlLockService $lockService) {}
$lockService->acquire('my-lock', timeout: 5);
$hasLock = $lockService->hasLock('my-lock');
$lockService->release('my-lock');
$lockService->acquireLocks(['lock-a', 'lock-b'], timeout: 5);
$lockService->releaseLocks(['lock-a', 'lock-b']);
$lockService->releaseLocks();
use PrecisionSoft\Doctrine\Utility\Entity\CreatedTrait;
class MyEntity
{
use CreatedTrait;
}
use Doctrine\ORM\Mapping as ORM;
use PrecisionSoft\Doctrine\Utility\Entity\ModifiedTrait;
#[ORM\HasLifecycleCallbacks]
class MyEntity
{
use ModifiedTrait;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.