Привет, я использую 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);
Я получаю эту ошибку.
[Syntax Error] 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





В вашем присоединении $qb->leftJoin('c.model', 'm.id'); часть m.id теперь является вашим псевдонимом. Меняем его на $qb->leftJoin('c.model', 'm');
Рад, что смог помочь!