执行时出现异常'SELECT count(DISTINCT o0_.id) AS sclr_0 FROM Order o0_'

An exception occurred while executing 'SELECT count(DISTINCT o0_.id) AS sclr_0 FROM Order o0_'

在 Symfony 3.4.8 上使用 SonataAdminBundle 时遇到以下问题:

当我查看我的 table 的列表时,出现以下错误。

An exception occurred while executing 'SELECT count(DISTINCT o0_.id) AS sclr_0 FROM Order o0_':

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order o0_' at line 1

我的Entity\Order.php

<?php

namespace Gobusgo\GobusgoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Order
 *
 * @ORM\Table(name="Order")
 *  @ORM\Entity(repositoryClass="Gobusgo\GobusgoBundle\Repository\OrderRepository")
 */
class Order
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\User")
     */
    protected $userId;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\Cargo")
     */
    protected $cargoId;

    /**
     * @var float
     *
     * @ORM\Column(type="float")
     */
    protected $price;

    /**
     * @var float
     *
     * @ORM\Column(name="quantity_of_cargo", type="float")
     */
    protected $quantityOfCargo;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\Address")
     *
     */
    protected $shippingAddress;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\Address")
     *
     */
    protected $deliveryAddress;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\Address")
     *
     */
    protected $additionalAddress1;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\Address")
     *
     */
    protected $additionalAddress2;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\Address")
     *
     */
    protected $additionalAddress3;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\Address")
     *
     */
    protected $additionalAddress4;

    /**
     * @ORM\ManyToOne(targetEntity="Gobusgo\GobusgoBundle\Entity\Address")
     *
     */
    protected $additionalAddress5;

    /**
     *
     *
     * @ORM\Column(type="datetime")
     */
    protected $dateOfOrder;

    /**
     * @var float
     *
     * @ORM\Column(type="float")
     */
    protected $status;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * @param mixed $userId
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;
    }

    /**
     * @return mixed
     */
    public function getCargoId()
    {
        return $this->cargoId;
    }

    /**
     * @param mixed $cargoId
     */
    public function setCargoId($cargoId)
    {
        $this->cargoId = $cargoId;
    }

    /**
     * @return float
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * @param float $price
     */
    public function setPrice($price)
    {
        $this->price = $price;
    }

    /**
     * @return float
     */
    public function getQuantityOfCargo()
    {
        return $this->quantityOfCargo;
    }

    /**
     * @param float $quantityOfCargo
     */
    public function setQuantityOfCargo($quantityOfCargo)
    {
        $this->quantityOfCargo = $quantityOfCargo;
    }

    /**
     * @return mixed
     */
    public function getShippingAddress()
    {
        return $this->shippingAddress;
    }

    /**
     * @param mixed $shippingAddress
     */
    public function setShippingAddress($shippingAddress)
    {
        $this->shippingAddress = $shippingAddress;
    }

    /**
     * @return mixed
     */
    public function getDeliveryAddress()
    {
        return $this->deliveryAddress;
    }

    /**
     * @param mixed $deliveryAddress
     */
    public function setDeliveryAddress($deliveryAddress)
    {
        $this->deliveryAddress = $deliveryAddress;
    }

    /**
     * @return mixed
     */
    public function getAdditionalAddress1()
    {
        return $this->additionalAddress1;
    }

    /**
     * @param mixed $additionalAddress1
     */
    public function setAdditionalAddress1($additionalAddress1)
    {
        $this->additionalAddress1 = $additionalAddress1;
    }

    /**
     * @return mixed
     */
    public function getAdditionalAddress2()
    {
        return $this->additionalAddress2;
    }

    /**
     * @param mixed $additionalAddress2
     */
    public function setAdditionalAddress2($additionalAddress2)
    {
        $this->additionalAddress2 = $additionalAddress2;
    }

    /**
     * @return mixed
     */
    public function getAdditionalAddress3()
    {
        return $this->additionalAddress3;
    }

    /**
     * @param mixed $additionalAddress3
     */
    public function setAdditionalAddress3($additionalAddress3)
    {
        $this->additionalAddress3 = $additionalAddress3;
    }

    /**
     * @return mixed
     */
    public function getAdditionalAddress4()
    {
        return $this->additionalAddress4;
    }

    /**
     * @param mixed $additionalAddress4
     */
    public function setAdditionalAddress4($additionalAddress4)
    {
        $this->additionalAddress4 = $additionalAddress4;
    }

    /**
     * @return mixed
     */
    public function getAdditionalAddress5()
    {
        return $this->additionalAddress5;
    }

    /**
     * @param mixed $additionalAddress5
     */
    public function setAdditionalAddress5($additionalAddress5)
    {
        $this->additionalAddress5 = $additionalAddress5;
    }

    /**
     * @return mixed
     */
    public function getDateOfOrder()
    {
        return $this->dateOfOrder;
    }

    /**
     * @param mixed $dateOfOrder
     */
    public function setDateOfOrder($dateOfOrder)
    {
        $this->dateOfOrder = $dateOfOrder;
    }

    /**
     * @return float
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param float $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }



}

我的Admin\OrderAdmin.php

<?php

namespace Gobusgo\GobusgoBundle\Admin;

use Gobusgo\GobusgoBundle\Entity\Cargo;
use Gobusgo\GobusgoBundle\Entity\User;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;

class OrderAdmin extends AbstractAdmin
{

    protected $baseRouteName = 'order_admin';
    protected $baseRoutePattern = 'order_admin';

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('userId', ModelType::class,[
                'class'=>User::class,
                'property'=>'fullName',
            ])
            ->add('cargoId', ModelType::class, [
                'class'=>Cargo::class,
                'property'=>'name'
            ])
        ;
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper->add('id');
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper->addIdentifier('id');
    }
}

我的services.yml包含以下代码

admin.order:
    class: Gobusgo\GobusgoBundle\Admin\OrderAdmin
    arguments: [~, Gobusgo\GobusgoBundle\Entity\Order, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, label: Заказ , group: "app.admin.group.admin" }
    public: true

请帮帮我...

Doctrine 将您的 table 名称按原样添加到查询中,没有反引号。因此,mysql 判定这是语法错误,因为 ORDER BY 是不区分大小写的保留关键字 sql。

要解决此问题,您可以在映射中重命名 table,例如

@ORM\Table(name="Order_postfix")

您还可以通过在映射 they say 中使用反引号手动转义 table 名称。像这样:

@ORM\Table(name="`Order`")