PHP code example of team-mate-pro / doctrine-utils-bundle

1. Go to this page and download the library: Download team-mate-pro/doctrine-utils-bundle 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/ */

    

team-mate-pro / doctrine-utils-bundle example snippets


return [
    // ...
    TeamMatePro\DoctrineUtilsBundle\TeamMateProDoctrineUtilsBundle::class => ['all' => true],
];

use TeamMatePro\DoctrineUtilsBundle\Entity\File;

$file = new File(
    name: 'document.pdf',
    mime: 'application/pdf',
    bytes: filesize('/tmp/upload.pdf'),
    realPath: '/tmp/upload.pdf',
);

$entityManager->persist($file);
$entityManager->flush();



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use TeamMatePro\Contracts\Model\FileInterface;

#[ORM\Entity]
class CustomFile implements FileInterface
{
    // Implement 



namespace App\Factory;

use App\Entity\CustomFile;
use TeamMatePro\Contracts\Model\FileInterface;
use TeamMatePro\DoctrineUtilsBundle\Factory\EntityFileFactoryInterface;

class CustomFileFactory implements EntityFileFactoryInterface
{
    public function createFromInterface(FileInterface $file): FileInterface
    {
        return new CustomFile(
            // map properties from $file
        );
    }
}

use TeamMatePro\DoctrineUtilsBundle\Entity\File;

// Upload - file is automatically stored when entity is persisted
$file = new File(
    name: 'document.pdf',
    mime: 'application/pdf',
    bytes: filesize('/tmp/upload.pdf'),
    realPath: '/tmp/upload.pdf',
);

$entityManager->persist($file);
$entityManager->flush();
// File is now in your Flysystem storage with key = $file->getId()

// Delete - file is automatically removed when entity is deleted
$entityManager->remove($file);
$entityManager->flush();
// File is removed from storage

public function testFileUpload(): void
{
    $file = new File();
    // ... set properties

    $this->entityManager->persist($file);
    $this->entityManager->flush();

    $storage = self::getContainer()->get('defaultStorage');
    $this->assertTrue($storage->fileExists($file->getId()));
}



namespace App\Entity;

use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use TeamMatePro\Contracts\Entity\TimeStampAbleInterface;

#[ORM\Entity]
class MyEntity implements TimeStampAbleInterface
{
    #[ORM\Column(type: 'datetime_immutable', nullable: true)]
    private ?DateTimeInterface $createdAt = null;

    #[ORM\Column(type: 'datetime_immutable', nullable: true)]
    private ?DateTimeInterface $updatedAt = null;

    public function getCreatedAt(): ?DateTimeInterface
    {
        return $this->createdAt;
    }

    public function getUpdatedAt(): ?DateTimeInterface
    {
        return $this->updatedAt;
    }

    public function timestamp(): void
    {
        $now = new DateTimeImmutable();

        if ($this->createdAt === null) {
            $this->createdAt = $now;
        }

        $this->updatedAt = $now;
    }
}



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use TeamMatePro\DoctrineUtilsBundle\Trait\AutoIncrementIdTrait;

#[ORM\Entity]
class MyEntity
{
    use AutoIncrementIdTrait;

    #[ORM\Column(type: 'string')]
    private string $name;

    // ... other properties
}



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
use TeamMatePro\DoctrineUtilsBundle\Trait\UuidIdTrait;

#[ORM\Entity]
class MyEntity
{
    use UuidIdTrait;

    #[ORM\Column(type: 'string')]
    private string $name;

    public function __construct()
    {
        $this->id = Uuid::v4();
    }

    // ... other properties
}

use function TeamMatePro\DoctrineUtilsBundle\Utils\binary;
use function TeamMatePro\DoctrineUtilsBundle\Utils\binaryUnwrap;

// Convert UUID string to binary (for Doctrine queries)
$binaryId = binary('550e8400-e29b-41d4-a716-446655440000');

// Convert multiple UUIDs to binary
$binaryIds = binary(['uuid1', 'uuid2', 'uuid3']);

// Convert binary back to UUID string
$uuidString = binaryUnwrap($binaryId);

use function TeamMatePro\DoctrineUtilsBundle\Utils\binary;

$qb = $entityManager->createQueryBuilder();
$qb->select('e')
   ->from(Entity::class, 'e')
   ->where('e.id IN (:ids)')
   ->setParameter('ids', binary($uuidStrings));
bash
php bin/console debug:container EntityFileFactoryInterface
bash
php bin/console debug:event-dispatcher doctrine.event_listener