我希望能够在一个实体上实现动态实体映射,并将其用于其他实体。例如,我有一个文件实体,它将存储MIME类型、映射键、名称等,还有一个包含它所属的实体ID的entity_id。映射键将确定类,因为这个文件实体将是多对多的。因此,文件实体的targetEntity并不固定。如何实现这一点?
文件实体
文件实体
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* File
*
* @ORM\Entity
*/
class File {
//.... Other mapping properties
/**
* @ORM\ManyToOne(targetEntity="SuperClass", inversedBy="files")
* @ORM\JoinColumn(name="entity_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $entity;
}
Product Entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Entity
*/
class Product extends SuperClass {
//.... Other mapping properties
/**
* @ORM\OneToMany(targetEntity="File", mappedBy="entity")
*/
protected $files;
}
But I have many other entities similar to Product
and how do I make sure that when I call getFiles()
, I get the files of the respective entity. I think it may work like this anyway, but is this the right way, or is there a better way?