如何组织不寻常的连接OneToOne Doctrine2

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

存在一个实体,您需要在其中实现OneToOne关系,但是问题是,它们之间的连接是由不是关键的字段来执行的,并且其中有多个字段。

    class MyEntityOne
    {
        
        protected $ id;
        protected $ pole1;
        protected $ pole2;
        protected $ pole3;
        protected $ myEntytyTwo;
         
    }
    
    class MyEntityTwo
    {
        protected $ pole1;
        protected $ pole2;
        protected $ pole3;
        protected $ pole4;
        protected $ pole5;
        protected $ myEntytyOne;
    }

它们之间的关系是:

SELECT * FROM MyEntityOne O LEFT JOIN MyEntityTwo T ON (o.pole1 = t.pole1 and o.pole2 = t.pole2 and o.pole2 = t.pole2 and t.pole5 = 1)

我在文档中没有找到如何更好地实现此连接的方法,我将不胜感激。

php doctrine
1个回答
0
投票

这应该可以解决问题

        /** @var EntityManagerInterface */
        $em = $this->getDoctrine()->getManager();

        $qb = $em->createQueryBuilder();
        $qb->select('o')
            ->from('App\Entity\MyEntityOne', 'o')
            ->join('App\Entity\MyEntityTwo', 't')
            ->where(
                'o.pole1 = t.pole1',
                'o.pole2 = t.pole2',
                'o.pole3 = t.pole3',
                't.pole5 = 1'
            )
            ->getQuery()
            ->getResult()
        ;
© www.soinside.com 2019 - 2024. All rights reserved.