How do I persist Images with Doctrine

2019-05-28 04:12发布

I want to persist an Image in my Article Entity.

Is this possible with Doctrine2?

How do i do this?

Best Regards, bodo

1条回答
乱世女痞
2楼-- · 2019-05-28 04:29

This is pretty simple using the ODM setup with the @File type.

For a ORM entity, I would look at creating a column like so.

NOTE: Use this link to add the blob type to Doctrine2

How add BLOB type in Doctrine 2 using Symfony 2

/** @ORM\Column(type="blob") */
protected $imageData;

/** @ORM\Column(type="string") */
protected $imageType;

/** @ORM\Column(type="int") */
protected $imageLength;

/** @ORM\Column(type="int") */
protected $imageWidth;

/** @ORM\Column(type="int") */
protected $imageHeight;

Then set that property with the raw data of the image. You will want to store things like the length, content-type, width and height along with the raw data so that you can create the right headers to retrieve the image and display it in the browser or download it.

That should be enough to get you started on the right path, or decide not to bother.

查看更多
登录 后发表回答