Search code examples
symfonysymfony-cmf

Symfony CMF - Uploading Images and PDF Files


I am using the Symfony CMF Media Bundle to achieve the following. I am having several nodes that can have an image and a downloadable PDF. I have already figured out that the setImage method has to be implemented like that:

public function setPreviewImage($previewImage)
{
    if ($previewImage === null) {
        return $this;
    }

    if (!$previewImage instanceof ImageInterface && !$previewImage instanceof UploadedFile) {
        $type = is_object($previewImage) ? get_class($previewImage) : gettype($previewImage);
        throw new \InvalidArgumentException(sprintf(
            'Image is not a valid type, "%s" given.',
            $type
        ));
    }

    if ($this->previewImage) {
        $this->previewImage->copyContentFromFile($previewImage);
    } elseif ($previewImage instanceof ImageInterface) {
        $previewImage->setName('previewImage');
        $this->previewImage = $previewImage;
    } else {
        $this->previewImage = new Image();
        $this->previewImage->copyContentFromFile($previewImage);
    }

    return $this;
}

Then in another forum someone was suggested to make this property cascade-persistent. with that hint: https://github.com/symfony-cmf/BlockBundle/blob/master/Resources/config/doctrine-phpcr/ImagineBlock.phpcr.xml#L22. Now i am wondering how and were i can set this option in my configuration.

The next part i am wondering about is the cmf_media_file type. Has anyone out here ever managed to store a PDF into a PHPCR node property?

For any help i would be really thankful.


Solution

  • I figured it out by myself. For anyone who is using annotations you have to set it up like this:

    use Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Image;
    use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
    
    /** 
     * @var Image
     * @PHPCR\Child(cascade="persist")
     */