【问题标题】:Syntax Error - line 0, col 50: Error: Expected end of string, got '.'"语法错误 - 第 0 行,第 50 列:错误:预期的字符串结尾,得到 '.'"
【发布时间】:2018-12-18 01:52:15
【问题描述】:

您好,我正在使用 symfony 4 和教义。这是我的教义查询。

public function paginationQueryByAttributes($attributes) {

        $qb = $this->createQueryBuilder("c");
        $qb->select('c');

        if (array_key_exists('model_id', $attributes)) {
            $qb->leftJoin('c.model', 'm.id');
            $qb->andWhere('m.id = ' . $attributes['model_id']);
            unset($attributes['model_id']);
        } elseif (array_key_exists('brand_id', $attributes)) {
            $qb->leftJoin('c.model', 'm');
        }
}

这是我的汽车模型。

<?php

namespace App\Entity;

use App\Model\BaseCar;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Swagger\Annotations as SWG;
use JMS\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CarRepository")
 * @UniqueEntity("id")
 */
class Car extends BaseCar
{
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Model",cascade={"refresh","merge"}, inversedBy="cars")
     * @ORM\JoinColumn(nullable=false)
     */
    protected $model;

    public function getModel()
    {
        return $this->model;
    }

    public /**
 * @param $
 */function setModel( $model)
    {
        $this->model = $model;

        return $this;
    }
}

我这样调用存储库,

$attributes = $request->request->all();

        $query = $this->getDoctrine()
                ->getRepository(Car::class)
                ->paginationQueryByAttributes($attributes);

我收到了这个错误。

[语法错误] line 0, col 50: Error: Expected end of string, got '.'

这是查询...

 SELECT c 
 FROM App\Entity\Car c 
   LEFT JOIN c.model m.id 
   LEFT JOIN m.brand b 
 WHERE m.id = 2 
   AND b.id = 1 
   AND c.code like :code 
   AND c.name like :name

【问题讨论】:

    标签: doctrine symfony4


    【解决方案1】:

    在您的加入 $qb-&gt;leftJoin('c.model', 'm.id'); 中,m.id 部分现在是您的别名。改成$qb-&gt;leftJoin('c.model', 'm');

    【讨论】:

    • 很高兴我能帮上忙!
    猜你喜欢
    • 2019-12-20
    • 2021-07-23
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2014-03-03
    • 2016-11-09
    • 2019-01-24
    • 2020-01-02
    相关资源
    最近更新 更多