gpt4 book ai didi

php - Doctrine Join Many To Many without association

转载 作者:可可西里 更新时间:2023-11-01 13:32:21 25 4
gpt4 key购买 nike

我有:两个具有单向 M:M 关联的实体。

class ShareInfo
{
// ...

/**
* @ORM\ManyToMany(targetEntity="Item")
* @ORM\JoinTable(name="share_info_items",
* joinColumns={@ORM\JoinColumn(name="share_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id")})
*
* @var Item[]
*/
private $items;
}

class Item
{
// ...

// This entity has no association with ShareInfo,
// because M:M is undirectional and defined in ShareInfo entity
}

我想要的:从items表(Item实体)中选取数据,其中Item和ShareInfo之间至少存在一条M:M记录。

我的建议不起作用(我有一个语义错误):

$queryBuilder
->select('i')
->from(Item::class, 'i')
->innerJoin(ShareInfo::class, 'shareInfo', 'WITH', 'shareInfo.items = i');

在纯 SQL 中,我会做这样的事情:

SELECT i.*
FROM items i
INNER JOIN share_info_items shareInfo
ON shareInfo.item_id = i.id

不敢相信没有 DQL 模拟这个。我能想到的唯一解决方案是将单向 M:M 关联拆分为双向

附言这道题没有重复,我检查的很好。

最佳答案

实现这一点的方法是通过子查询:

$em=$this->getDoctrine()->getManager();
$queryBuilder1=$em->createQueryBuilder();
$queryBuilder1->select(array('DISTINCT i.id'))
->from('AppBundle:ShareInfo', 'share_info')
->innerJoin('share_info.items', 'i');
$queryBuilder=$em->createQueryBuilder();
$queryBuilder->select('i')
->from('AppBundle:items', 'i')
->where($queryBuilder->expr()
->in('i.id',$queryBuilder1->getDql()));

关于php - Doctrine Join Many To Many without association,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30063303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com