gpt4 book ai didi

sql - Symfony2 在自定义存储库类中使用查询

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

我有一个带有调用自定义查询的方法的存储库类。当我尝试调用 findAllWithRating()从 Controller 内部我得到以下异常:

[2/2] QueryException: [Syntax Error] line 0, col 156: Error: Unexpected 'NULL'

如果我尝试在 phpmyadmin 中调用查询,则查询效果很好!

任何的想法?
<?php
namespace Anchorbrands\Bundle\MessageBundle\Entity;

use Doctrine\ORM\EntityRepository;

class MessageRepository extends EntityRepository
{

public function findAllWithRating() {
return $this->getEntityManager()
->createQuery("SELECT id, user_id, title, slug, content, question, image_name, created_at, updated_at,
MAX(CASE WHEN rating = '1' THEN totalCount ELSE NULL END) 'Rating 1',
MAX(CASE WHEN rating = '2' THEN totalCount ELSE NULL END) 'Rating 2',
MAX(CASE WHEN rating = '3' THEN totalCount ELSE NULL END) 'Rating 3'
FROM
(
SELECT a.id, a.user_id, a.title, a.slug , a.content, a.question, a.image_name, a.created_at, a.updated_at,
rating, COUNT(*) totalCount
FROM AnchorbrandsMessageBundle:Message a
LEFT JOIN AnchorbrandsMessageBundle:Rating b
ON a.id = message_id
GROUP BY a.id, a.user_id, a.title, a.slug, a.content, a.question, a.image_name, a.created_at, a.updated_at, rating
) r
GROUP BY id, user_id, title, slug, content, question, image_name, created_at, updated_at")->getResult();
}

}

最佳答案

您似乎混合了 DQL 和 SQL。您可以查看 DQL here 的可能性.

查看您的查询,我建议您使用 SQL 而不是 DQL。

如何在存储库中执行 SQL 查询的示例:

$sql = 'SELECT * FROM table WHERE id = :id';
$params = array(
'id' => 4,
);

return $this->getEntityManager()->getConnection()->executeQuery($sql, $params)->fetchAll();

关于sql - Symfony2 在自定义存储库类中使用查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14399935/

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