gpt4 book ai didi

php - 根据另一个实体字段值查询一个实体

转载 作者:行者123 更新时间:2023-11-29 02:54:32 25 4
gpt4 key购买 nike

我在两个实体之间有这种 OneToMany 关系:Sculpture(1) 和 Image(n)。我的目标是查询所有 Sculptures 的所有 Image.featured 设置为 0。如果 Sculpture 至少有一个 Image 具有 featured = 1,则查询不应检索它(按设计,只有一个雕塑的图像无论如何都可以作为特色)。

这里是生成的表:

CREATE TABLE IF NOT EXISTS `image` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sculpture_id` int(11) DEFAULT NULL,
`nom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`featured` tinyint(1) NOT NULL,
`type` enum('mini','normal') COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_C53D045FB2720858` (`sculpture_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE IF NOT EXISTS `sculpture` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`titre` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`reference` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`largeur` int(11) NOT NULL,
`hauteur` int(11) NOT NULL,
`annee` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`matiere` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`active` tinyint(1) NOT NULL,
`creation` datetime NOT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`hits` int(11) NOT NULL,
`taille` enum('xs','s','m','l','xl') COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `image`
ADD CONSTRAINT `FK_C53D045FB2720858` FOREIGN KEY (`sculpture_id`) REFERENCES `sculpture` (`id`);

我尝试使用此 Repository 方法查询 Sculpture 实体:

class SculptureRepository extends EntityRepository
{
public function findByFeatured($featured)
{
$query = $this->createQueryBuilder('s')
->leftJoin('AppBundle\Entity\Image', 'i', 'WITH', 'i.sculpture = s.id')
->where('i.featured = :featured')
->setParameter('featured', $featured)
->orderBy('s.id', 'DESC')
->groupBy('s')
->getQuery()
;

return $query->getResult();
}
}

并使用此 Repository 方法查询 Image 实体:

class ImageRepository extends EntityRepository
{
public function findNoFeatured()
{
$query = $this->createQueryBuilder('i')
->where('i.featured = 0')
->groupBy('i.sculpture')
->getQuery();

return $query->getResult();

}
}

但是当我只想要没有特色图像的那些时,这些会返回所有Sculptures

有什么想法吗?

谢谢!

最佳答案

像这样:

$query = $this->createQueryBuilder('s, count(i.id) as featured_image_count')
->leftJoin('AppBundle\Entity\Image', 'i', 'WITH', 'i.sculpture = s.id')
->where('i.featured = :featured')
->setParameter('featured', 1)
->orderBy('s.id', 'DESC')
->groupBy('s')
->having('featured_image_count < 1')
->getQuery()
;

或者,您可以使用子查询来获取所有 featured == 1 的图像,然后您可以使用 not in 来消除所有这些雕塑,例如:

$qb = $this->createQueryBuilder();
$qb2 = $qb;
$qb2->select('i.sculptureId')->distinct(true)
->from('AppBundle\Entity\Image', 'i')
->where('i.featured = 1');

$qb = $this->createQueryBuilder();
$qb->select('s')
->from('AppBundle\Entity\Sculpture', 's')
->where($qb->expr()->notIn('s.id', $qb2->getDQL())
);

$result = $qb->getQuery()->getResult();

我没有对这些进行任何语法检查,但这两种方法都可以正常工作。

关于php - 根据另一个实体字段值查询一个实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32086846/

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