简体   繁体   English

动态目标实体Doctrine 2和Symfony 2

[英]Dynamic Target entity Doctrine 2 and Symfony 2

I want dynamic entity mapping on one entity which will be used by other entities. 我希望在一个实体上进行动态实体映射,这将由其他实体使用。 For example, I have a File entity, which will store MIME type, mapping key , name etc, also an entity_id which will contain the id to the entity it belongs to. 例如,我有一个File实体,它将存储MIME类型,映射keyname等,还有一个entity_id ,它将包含它所属实体的id。 The mapping key will determine the class as this file entity will be many-to-many. 映射key将确定类,因为此文件实体将是多对多的。 So the targetEntity for File entity isn't fixed. 因此, targetEntity for File实体不是固定的。 How to achieve that? 怎么实现呢?

File Entity 文件实体

<?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. 但我有很多其他类似于Product实体,我如何确保在调用getFiles() ,我得到相应实体的文件。 I think it may work like this anyway, but is this the right way, or is there a better way? 我认为它可能会像这样工作,但这是正确的方式,还是有更好的方法?

Ok, this is a very common problem I have - how to link one entity to many others which are very similar. 好吧,这是一个非常常见的问题 - 如何将一个实体链接到许多非常相似的实体。

First, reevaluate: I often find, that I don't actually need that many different entities and that they can be folded into one there by simplifying everything. 首先,重新评估:我经常发现,我实际上并不需要那么多不同的实体,并且可以通过简化所有内容将它们折叠成一个实体。 We often overengineer and overcomplicate when we don't really have to (or even shouldn't)! 当我们不必(甚至不应该)时,我们经常过度工作和过度复杂!

If that's not an option: create separate relationships between File and all other (relevant) entities. 如果这不是一个选项:在File和所有其他(相关)实体之间创建单独的关系。 Yes, your file entity will be full of relationships. 是的,您的文件实体将充满关系。 Then write one general access getter that computes which entity it is connected to and returning it (either by going through all connections or it can be more involved that that [and faster] by computing it from other params (like mime type in your case). This way you hide the complexity of all those relationsip from the rest of your code. 然后编写一个通用访问getter,计算它连接到哪个实体并返回它(通过遍历所有连接,或者通过从其他params计算它[和更快]可以更多地参与(如你的情况下的mime类型)这样你就可以隐藏所有这些关系的复杂性。

This option works if you just need a good enough solution. 如果您只需要一个足够好的解决方案,此选项有效。 A more correct one would be to forgo doctrine relations and track IDs and related entities classes in two fields and select and instantiate the objects yourself (in the entity, ofcorse). 更正确的一种方法是放弃学说关系并在两个字段中跟踪ID和相关实体类,并自己选择和实例化对象(在实体中,ofcorse)。 But that's more involved and more error prone. 但这更复杂,更容易出错。

If you find a way to do this in a more elegant, doctrine2 backed way, please do let me (and the rest of us) know. 如果你找到一种方法以更优雅,更合理的方式做到这一点,请让我(和我们其他人)知道。

I found out that according to my requirements, I can do with ManyToMany Unidirectional or OneToMany Unidirectional. 我发现根据我的要求,我可以使用ManyToMany Unidirectional或OneToMany Unidirectional。 But for bidirectional, there seems to be no solution. 但对于双向,似乎没有解决方案。 I have something in mind, but it will just complicate the Scenario. 我有一些想法,但它只会使场景复杂化。 According to doctrines documentation, the schema Generated will be 根据学说文档,生成的模式将是

CREATE TABLE Product (
    id INT AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;

CREATE TABLE Feature (
    id INT AUTO_INCREMENT NOT NULL,
    product_id INT DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;

ALTER TABLE Feature ADD FOREIGN KEY (product_id) REFERENCES Product(id);

So like I suggested in my question to extend a SuperClass, what if the super class to be an entity itself with its own table, containing a field which will store repository path, and somehow it will be processed to return the proper entity. 所以就像我在我的问题中建议扩展一个SuperClass一样,如果超类是一个拥有自己的表的实体本身,包含一个将存储存储库路径的字段,以及某种方式它将被处理以返回正确的实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM