Doctrine ORM Association映射两列

问题描述 投票:-1回答:1

我想从多个实体到一个实现一对多的关联。

Entity_1,Entity_2,Entity_3,Entity _... all对于Entity_A应该具有一对多。

但这在Doctrine中是不可能的:

有解决方法吗?什么是最好的方式?

php symfony doctrine-orm doctrine
1个回答
0
投票

你需要ManyToOne // OneToMany

但是如果你没有主键需要主键,你需要使用连接构建自定义查询

    manyToOne:
        nameforthepoint:
            targetEntity: EntityDestinyName
            cascade: {  }
            fetch: LAZY
            mappedBy: null
            inversedBy: null (depends bidirectional)
            joinColumns:
                name_of_the_origin_column:
                    referencedPropertyEntityName: name_of_the_destiny_column
            orphanRemoval: false

如果您没有通过主键关联(例如Symfony Doctrine查询加入):

        $em = $this->getEntityManager();
        $db = $em->getConnection();

        $query = "SELECT * FROM  table as p1
            INNER JOIN table2 AS p2 ON p2.column = p1.column";

        $stmt   = $db->prepare($query);
        $params = array();
        $stmt->execute($params);

        $resultset = $stmt->fetchAll();

希望它对您有所帮助,如果您需要其他信息,请询问/评论。

© www.soinside.com 2019 - 2024. All rights reserved.