gpt4 book ai didi

php - 带连接表的 Doctrine 查询构建器 ManyToMany

转载 作者:行者123 更新时间:2023-11-29 17:39:22 24 4
gpt4 key购买 nike

我尝试通过学说查询构建器构建类似这样的查询:

SELECT m.id
FROM product p, model m
INNER JOIN product_model pm ON (m.id = pm.model_id)
WHERE p.id = pm.product_id AND m.serial = ? AND p.id = ?

product_model是“ManyToMany单向关系”中的JoinTable。我使用下面的代码:

$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder
->select('m.id')
->from(Model::class, 'm')
->join(Product::class, 'p', Join::ON, '1 = 1')
->where('m.serial.serial = :serial')
->andWhere('p.productId.id = :productId')
->setParameters([
'serial' => (string)$serial,
'productId' => $productId->value(),
]);
$query = $queryBuilder->getQuery();
$results = $query->getOneOrNullResult();

但是当我尝试转储此查询的 SQL 时,我得到:

SELECT m0_.id AS id_0 
FROM model m0_
INNER JOIN product p1_ ON (1 = 1)
WHERE m0_.serial = ? AND p1_.id = ?

这个查询的结果不是我所期望的

最佳答案

我找到了解决方案。我需要另一个具有复合键的产品模型表实体类,并且查询中的变化很小:

$queryBuilder
->select('m.id')
->from(Model::class, 'm')
->join(ProductModel::class, 'pm', Join::WITH, $queryBuilder->expr()->eq('m.id', 'pm.modelId'))
->where('m.serial.serial = :serial')
->andWhere('pm.productId = :productId')
->setParameters(
[
'serial' => (string)$serial,
'productId' => $productId->value(),
]
);

关于php - 带连接表的 Doctrine 查询构建器 ManyToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50079956/

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