【发布时间】:2019-08-28 12:26:26
【问题描述】:
是否可以在父实体中定义 OneToMany 或 OneToOne 关系的映射?我暂时没有找到任何关于此的文档。
实际完成:
实体;
/**
* @ORM\OneToOne(targetEntity="Asset", cascade={"persist", "remove"})
*/
private $image;
资产:
class Asset
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $imageName;
实体类型:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('aaset', AssetType::class, [
'label' => 'label'
])
资产类型:
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('imageFile', VichImageType::class, [
'label' => 'Image',
'required' => false,
'allow_delete' => false,
'download_uri' => false,
'image_uri' => true
])
资产的 VichUploader 定义:
App\Entity\Asset:
imageFile:
mapping: images
filename_property: imageName
VichUploader 实体定义:
App\Entity\Entity:
oneToOne:
image:
mapping: other_mapping
filename_property: imageName
有人已经解决了这个问题吗? 谢谢你的帮助。
托尼
【问题讨论】:
-
你到底想在这里做什么?没有描述清楚。所以
Entity就是User和Asset就是ProfilePicture?并且您想将User和ProfilePicture实体存储在单独的数据库表中,并在profile_pictures表中使用user_id之类的连接列?那么您想要一个可以更改ProfilePicture的User的表单吗?我猜你已经在使用表单来输入数据了。请将该表格添加到问题中! -
您好,目标是根据Entity Class定义VichUploader映射。我需要根据父类不同的文件夹。我只是在问题中添加表单类型。感谢您的帮助
-
所以根据我的例子,如果
ProfilePicture在User中使用,则上传的文件应存储在即uploads/user/profile_pictures中,如果ProfilePicture用于说Admin类图片应该去uploads/admin/profile_pictures,但你想重复使用ProfilePictureType表单和ProfilePicture类? -
您是否只是在问题中“发明”了
Entity的映射,以作为您想要实现的示例?捆绑包的“Configuration Reference”中没有oneToOne配置键。这就是我问的原因。 -
我只想重用 Asset 类,但使用不同的 VichUploader 映射。
标签: php symfony vichuploaderbundle