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

标签 php mysql symfony sonata-admin

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

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

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

我的实体\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 将您的表名按原样添加到查询中,没有反引号。因此,mysql 认为这是一个语法错误,因为 ORDER BY 是不区分大小写的保留 sql 关键字。

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

@ORM\Table(name="Order_postfix")

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

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

关于php - 'SELECT count(DISTINCT o0_.id) AS sclr_0 FROM Order o0_' 执行时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49815038/

相关文章:

javascript - 如何在我的 php mysql 中实现 Livestamp.js + Moment.js

mysql - 忽略具有零值的计数不适用于子查询

mysql - 以数值拆分/分解 mysql 字符串

php - 如何用PHP制作不规则表格?

javascript - Jquery/AJAX PHP 域检查器

php - Laravel 5.3 中的搜索栏以过滤表格

python - 在 mysql 中存储 python timedelta 值的最佳方法是什么?

Symfony2 - 从编译器 channel 访问内核

php - 无法配置 Symfony(第 3 方)包

symfony - 在 Controller Action 之前执行代码