timostamm/orm-resource

Store files in a Doctrine ORM database

v1.1.0 2023-04-17 08:21 UTC

This package is auto-updated.

Last update: 2024-12-17 12:02:22 UTC


README

build Packagist PHP Version GitHub tag License

Doesn't actually store the files in the database, but puts them in a storage directory and references them in the database.

This package uses timostamm/web-resource for file representation.

Files in the file system are never deleted.

Example

/** @ORM\Entity() */
class TestEntity
{

    /**
     * @ORM\Embedded(class = EmbeddedResource::class )
     */
    private $file;


    public function getFile(): ?ResourceInterface
    {
        return $this->file;
    }

    public function setFile(?ResourceInterface $resource): void
    {
        $this->file = EmbeddedResource::create($resource);
    }

}


$em->getEventManager()
    ->addEventSubscriber(new ORMResourceHandler(new HashStorage($storageDir)));

$e = new TestEntity();
$e->setFile(Resource::fromFile(__FILE__));

$em->persist($e);
$em->flush($e);