[英]Symfony : $query->getResult() returns nothing when a table is empty
I want to show in a list all the data stored in a specific table in my database. 我想在列表中显示存储在数据库中特定表中的所有数据。
After doing my query, I want to get the number of results to display them or not. 查询完后,我想获取结果数量以显示或不显示它们。
public function listUseCaseAction(Request $request) {
// Load use cases from database
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('AppBundle:TagUseCase');
$query = $repo->createQueryBuilder('case')
->select(array('case.code', 'case.name'))
->orderBy('case.name', 'ASC')
->getQuery();
if (count($query->getResult()) > 0) {
// display list
} else {
// message : "There is no use case."
}
}
The problem is that $query->getResult()
seems to not return anything when a table is empty. 问题是,当表为空时, $query->getResult()
似乎不返回任何内容。 Neither false nor null... 既不假也不为空...
It returns a number only when there is data. 仅在有数据时才返回数字。
DQL : DQL:
'SELECT case.code, case.name FROM AppBundle\Entity\TagUseCase case ORDER BY case.name ASC'
Note : I have to get a query because I use KnpPaginator. 注意:我必须查询,因为我使用的是KnpPaginator。 I tried $repository->findAll()
and it works even a table is empty (returns an empty array) but sortable columns doesn't work anymore. 我尝试了$repository->findAll()
,即使表是空的(返回一个空数组),它也可以工作,但是可排序的列不再起作用。 It works only with a query. 它仅适用于查询。
I also tried this but it doesn't work neither, it returns nothing when there is 0 result : 我也试过了,但是它都不起作用,当结果为0时什么也不返回:
$qb = $em->createQueryBuilder();
$qb->select($qb->expr()->count('case.code'));
$qb->from('AppBundle:TagUseCase','case');
$count = $qb->getQuery()->getSingleScalarResult();
My question is : How can I count results of a query (even if there is no result) ? 我的问题是: 如何计算查询结果(即使没有结果)?
If you use the query to iterate thought the list, it is better to use php count than an additional query. 如果使用查询来遍历列表,则使用php count比使用其他查询更好。
Result as object, count with php: 结果作为对象,用php计数:
$qb = $repo->createQueryBuilder('case')
->orderBy('case.name', 'ASC')
;
$result = $qb->getQuery()->getResult();
var_dump(count($result));
I found the problem ! 我发现了问题!
You should never use the string " case " in a query, it is a SQL reserved word !! 您永远不要在查询中使用字符串“ case ”,它是一个SQL 保留字 !
Instead of : 代替 :
$query = $repo->createQueryBuilder('case')
->select(array('case.code', 'case.name'))
->orderBy('case.name', 'ASC')
->getQuery();
Write : 写:
$query = $repo->createQueryBuilder('c')
->select(array('c.code', 'c.name'))
->orderBy('c.name', 'ASC')
->getQuery();
The error [Syntax Error] line 0, col 17: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_WHEN, got 'AppBundle\\Entity\\TagUseCase'
is definitively no useful at all... 错误[Syntax Error] line 0, col 17: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_WHEN, got 'AppBundle\\Entity\\TagUseCase'
绝对[Syntax Error] line 0, col 17: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_WHEN, got 'AppBundle\\Entity\\TagUseCase'
...
I suggest using Query::count()
method: 我建议使用Query::count()
方法:
if ($query->count()) > 0) {
// display list
} ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.