gpt4 book ai didi

symfony - 在 where 条件下的学说 ORM 计数数组集合

转载 作者:行者123 更新时间:2023-12-04 05:31:59 25 4
gpt4 key购买 nike

使用 Symfony2.8Doctrine 2.5 ,我想在 Doctrine ORM 查询中过滤 arraycollection 正好包含 3 个元素的所有数据集。

$em = $this->getDoctrine()->getManager();
$query = $em->getRepository("AppBundle:EduStructItem")
->createQueryBuilder('e')
->addSelect('COUNT(e.preconditions) AS HIDDEN numberpre')
->having('numberpre = 3')
->getQuery();
$res = $query->getResult();
dump($res);
foreach ($res as $entity){
print "title:".$entity->getTitle()."<br>";
dump($entity->getPreconditions()->toArray());
}
preconditions是一个包含先决条件集合的数组集合。

最后,我想获得恰好具有 3 个先决条件的所有结果。
此外,按 arraycollection 中的值数量排序也很棒(类似于 order by Count(e.preconditions) )。

由于使用了另一个包,我将学说从 2.5.2 降级到 2.5.0。我不认为这是我的问题的原因,但为了完整起见,这里是我的 Composer 表演的教义部分:
data-dog/pager-bundle                v0.2.4             Paginator bundle for symfony2 and doctrine orm, allows customization with filters and sorters
doctrine/annotations v1.2.7 Docblock Annotations Parser
doctrine/cache v1.5.2 Caching library offering an object-oriented API for many cache backends
doctrine/collections v1.3.0 Collections Abstraction library
doctrine/common v2.5.2 Common Library for Doctrine projects
doctrine/data-fixtures v1.1.1 Data Fixtures for all Doctrine Object Managers
doctrine/dbal v2.5.2 Database Abstraction Layer
doctrine/doctrine-bundle 1.6.1 Symfony DoctrineBundle
doctrine/doctrine-cache-bundle 1.2.2 Symfony Bundle for Doctrine Cache
doctrine/doctrine-fixtures-bundle 2.3.0 Symfony DoctrineFixturesBundle
doctrine/doctrine-migrations-bundle 1.1.1 Symfony DoctrineMigrationsBundle
doctrine/inflector v1.1.0 Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator 1.0.5 A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations v1.1.0 Database Schema migrations using Doctrine DBAL
doctrine/orm v2.5.0 Object-Relational-Mapper for PHP

这是一个测试实体:
<?php
// src/AppBundle/Entity/EduStructItem.php
namespace AppBundle\Entity;


use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
* @ORM\Table(name="test_edustructitemcollection")
*/
class EduStructItem
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;


/**
* @var string
* @Assert\NotBlank()
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $title;


/**
* Preconditions are EduStructItems referencing to an EduStructItem.
* For a single EduStructItem its empty (which have no subelements).
* A join table holds the references of a main EduStructItem to its sub-EduStructItems (preconditions)
*
* @ORM\ManyToMany(targetEntity="EduStructItem",indexBy="id", cascade={"persist"})
* @ORM\JoinTable(name="test_edustructitem_preconditioncollection",
* joinColumns={@ORM\JoinColumn(name="edustructitem_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="edustructitem_precondition_id", referencedColumnName="id")}
* )
*/
public $preconditions;

public function __construct()
{
$this->preconditions = new ArrayCollection();
}

public function getTitle()
{
return $this->title;
}

public function setTitle($title)
{
$this->title = $title;
}


public function getPreconditions()
{
return $this->preconditions;
}

public function addPrecondition(\AppBundle\Entity\EduStructItem $precondition)
{
$this->preconditions->add($precondition);
}

public function removePrecondition(\AppBundle\Entity\EduStructItem $precondition)
{
$this->preconditions->removeElement($precondition);
}

}
?>

最后我总是得到错误:
[语义错误] 'preconditions)' 附近的第 0 行第 18 列:错误:无效的 PathExpression。预期的 StateFieldPathExpression 或 SingleValuedAssociationField。

现在我尝试了你的新解决方案:
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository("AppBundle:EduStructItem")
->createQueryBuilder('e')
->addSelect('COUNT(e.preconditions) AS HIDDEN countpre')
->join('e.preconditions', 'precondition', Join::WITH)
->having('countpre = 1')
->getQuery();

并再次得到错误:
[语义错误] 'preconditions)' 附近的第 0 行第 18 列:错误:无效的 PathExpression。预期的 StateFieldPathExpression 或 SingleValuedAssociationField。 Wenn 我在 HIDDEN 之前写了别名,我也得到了:[语义错误] line 0, col 53 near 'FROM AppBundle\Entity\EduStructItem': Error: Class 'FROM' is not defined。考虑到它是一种自反射(reflect)关系,只有一个实体但有两个表。正如您在我的实体的注释中所看到的,自我关系保存在 test_edustructitem_preconditioncollection - 表中,该表由于注释而由学说生成。

我尝试了您的最新解决方案:
$qb = $em->getRepository("AppBundle:EduStructItem")
->createQueryBuilder('item');
$qb->addSelect('COUNT(precondition.id) AS countpre HIDDEN ')
->join('item.preconditions', 'precondition', Join::WITH)
->having('countpre = 1');

当我在 HIDDEN 之前使用 countpre 时,我总是收到此错误:
[语义错误] 'FROM AppBundle\Entity\EduStructItem' 附近的第 0 行第 56 列:错误:未定义类 'FROM'。

但是当我把 countpre 放在 HIDDEN 之后:
$qb = $em->getRepository("AppBundle:EduStructItem")
->createQueryBuilder('item');
$qb->addSelect('COUNT(precondition.id) AS HIDDEN countpre')
->join('item.preconditions', 'precondition', Join::WITH)
->having('countpre = 1');

我收到错误:
执行 'SELECT t0_.id AS id_0, t0_.title AS title_1, COUNT(t1_.id) AS sclr_2 FROM test_edustructitemcollection t0_INNER JOIN test_edustructitem_preconditioncollection t2_ON t0_.id = t2_.edustructitemIN_1_0_.edustructitem_1. = t2_.edustructitem_precondition_id HAVING sclr_2 = 1':

SQLSTATE [42S22, 207]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Ungültiger Spaltenname 'sclr_2'。

500 内部服务器错误 - DBALException

1 个链接异常:SQLSrvException »

请考虑只有一个具有自引用的实体,并且有以下两个表:
USE [easylearndev4_rsc]
GO
/****** Object: Table [dbo].[test_edustructitemcollection] Script Date: 14.12.2015 09:31:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test_edustructitemcollection](
[id] [int] IDENTITY(1,1) NOT NULL,
[title] [nvarchar](255) NOT NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO


USE [easylearndev4_rsc]
GO
/****** Object: Table [dbo].[test_edustructitem_preconditioncollection] Script Date: 14.12.2015 09:32:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
REATE TABLE [dbo].[test_edustructitem_preconditioncollection](
[edustructitem_id] [int] NOT NULL,
[edustructitem_precondition_id] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[edustructitem_id] ASC,
[edustructitem_precondition_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[test_edustructitem_preconditioncollection] WITH CHECK ADD CONSTRAINT [FK_34E716A81B7A6CEB] FOREIGN KEY([edustructitem_precondition_id])
REFERENCES [dbo].[test_edustructitemcollection] ([id])
GO
ALTER TABLE [dbo].[test_edustructitem_preconditioncollection] CHECK CONSTRAINT [FK_34E716A81B7A6CEB]
GO
ALTER TABLE [dbo].[test_edustructitem_preconditioncollection] WITH CHECK ADD CONSTRAINT [FK_34E716A85D864668] FOREIGN KEY([edustructitem_id])
REFERENCES [dbo].[test_edustructitemcollection] ([id])
GO
ALTER TABLE [dbo].[test_edustructitem_preconditioncollection] CHECK CONSTRAINT [FK_34E716A85D864668]
GO

最后我自己找到了一个解决方法:
$em = $this->getDoctrine()->getManager();
$qb = $em->getRepository("AppBundle:EduStructItem")
->createQueryBuilder('e');
$qb->join('e.preconditions', 'p', Join::WITH)
->groupBy('e.id, e.title')
->having('count(p.id) = 1');

但是我对此不太满意,因为 arraycollection 已经是聚合数据,为什么我还要加入、计数和分组!
这不可能是教义的想法!
有谁知道更好的解决方案?

最佳答案

尝试这个:

$qb = $this->getDoctrine()->getManager()->getRepository("MyBundle:Groups")
->createQueryBuilder('g')
->having('count(g.members) = 3')
->orderBy('g.members', 'DESC')
;

关于symfony - 在 where 条件下的学说 ORM 计数数组集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34174574/

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