Symfony/Doctrine - QueryException - [Semantical Error] line 0, col 48 near 'pizza-mia': Error: 'pizza' is not defined
Symfony/Doctrine - QueryException - [Semantical Error] line 0, col 48 near 'pizza-mia': Error: 'pizza' is not defined
我想使用 DQL 在 SQLite 中进行此查询
//// src/Repository/PostRepository.php
public function hasPreviousPost($id,$slug): array
{
$em = $this->getEntityManager();
$query = $em->createQuery('SELECT p FROM App\Entity\Post p WHERE p.id < '.$id.' AND p.slug = '.$slug);
$posts = $query->getResult();
return $posts;
}
然后我从 PostController 中调用这个函数,就像那样
//// src/Controller/PostController.php
$posts = $this->getDoctrine()->getRepository(Post::class)->findAll();
foreach ($posts as $post){
$hasPreviousPost = $this->getDoctrine()->getRepository(Post::class)->hasPreviousPost($post->id(),$post->Slug());
}
但是当我 运行 代码时,我得到这个错误
“ [语义错误] 第 0 行,'pizza-mia' 附近的第 48 列:错误:'pizza' 未定义。“
pizza-mia 是 $slug 包含的字符串。
这是我的 Post 实体。
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
$countPosts = 0;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
* @ORM\Table(name="post")
*/
class Post
{
/**
* @ORM\Column(type="integer")
* @ORM\Column(unique=true)
*/
private $id;
/**
* @ORM\Id
* @ORM\Column(type="text")
* @ORM\Column(unique=true)
*/
private $postId;
/**
* @ORM\Column(type="text")
*/
private $slug;
/**
* @ORM\Column(type="text")
*/
private $message;
/**
* @ORM\Column(type="boolean",nullable=true)
*/
private $isScheduled;
/**
* @ORM\Column(type="integer")
*/
private $scheduledPublishTime;
function __construct($postId,$slug,$message)
{
global $countPosts;
$countPosts++;
$this->postId=$postId;
$this->id=$countPosts;
$this->slug=$slug;
$this->message=$message;
$this->isScheduled=false;
$this->scheduledPublishTime=0;
}
public function PostId(): ?string
{
return $this->postId;
}
public function id(): ?int
{
return $this->id;
}
public function Slug(): ?string
{
return $this->slug;
}
public function Message(): ?string
{
return $this->message;
}
public function IsScheduled(): ?bool
{
return $this->isScheduled;
}
public function changeIsScheduled(?bool $isScheduled): self
{
$this->isScheduled = $isScheduled;
return $this;
}
public function ScheduledPublishTime(): ?int
{
return $this->scheduledPublishTime;
}
public function changeScheduledPublishTime(int $scheduledPublishTime): self
{
$this->scheduledPublishTime = $scheduledPublishTime;
return $this;
}
}
如有任何帮助,我们将不胜感激。
这样试试。
$query = $em->createQuery('SELECT p FROM App\Entity\Post p WHERE p.id < :id AND p.slug = :slug');
$query->setParameter('id', $id);
$query->setParameter('slug', $slug);
:id
和:slug
代表命名参数
我的强烈假设是您只是缺少“ ”以在查询中将 $slug 定义为字符串。
但是使用像上面这样的参数也使它更具可读性。
中查找有关命名或编号参数的说明
我想使用 DQL 在 SQLite 中进行此查询
//// src/Repository/PostRepository.php
public function hasPreviousPost($id,$slug): array
{
$em = $this->getEntityManager();
$query = $em->createQuery('SELECT p FROM App\Entity\Post p WHERE p.id < '.$id.' AND p.slug = '.$slug);
$posts = $query->getResult();
return $posts;
}
然后我从 PostController 中调用这个函数,就像那样
//// src/Controller/PostController.php
$posts = $this->getDoctrine()->getRepository(Post::class)->findAll();
foreach ($posts as $post){
$hasPreviousPost = $this->getDoctrine()->getRepository(Post::class)->hasPreviousPost($post->id(),$post->Slug());
}
但是当我 运行 代码时,我得到这个错误 “ [语义错误] 第 0 行,'pizza-mia' 附近的第 48 列:错误:'pizza' 未定义。“
pizza-mia 是 $slug 包含的字符串。
这是我的 Post 实体。
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
$countPosts = 0;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
* @ORM\Table(name="post")
*/
class Post
{
/**
* @ORM\Column(type="integer")
* @ORM\Column(unique=true)
*/
private $id;
/**
* @ORM\Id
* @ORM\Column(type="text")
* @ORM\Column(unique=true)
*/
private $postId;
/**
* @ORM\Column(type="text")
*/
private $slug;
/**
* @ORM\Column(type="text")
*/
private $message;
/**
* @ORM\Column(type="boolean",nullable=true)
*/
private $isScheduled;
/**
* @ORM\Column(type="integer")
*/
private $scheduledPublishTime;
function __construct($postId,$slug,$message)
{
global $countPosts;
$countPosts++;
$this->postId=$postId;
$this->id=$countPosts;
$this->slug=$slug;
$this->message=$message;
$this->isScheduled=false;
$this->scheduledPublishTime=0;
}
public function PostId(): ?string
{
return $this->postId;
}
public function id(): ?int
{
return $this->id;
}
public function Slug(): ?string
{
return $this->slug;
}
public function Message(): ?string
{
return $this->message;
}
public function IsScheduled(): ?bool
{
return $this->isScheduled;
}
public function changeIsScheduled(?bool $isScheduled): self
{
$this->isScheduled = $isScheduled;
return $this;
}
public function ScheduledPublishTime(): ?int
{
return $this->scheduledPublishTime;
}
public function changeScheduledPublishTime(int $scheduledPublishTime): self
{
$this->scheduledPublishTime = $scheduledPublishTime;
return $this;
}
}
如有任何帮助,我们将不胜感激。
这样试试。
$query = $em->createQuery('SELECT p FROM App\Entity\Post p WHERE p.id < :id AND p.slug = :slug');
$query->setParameter('id', $id);
$query->setParameter('slug', $slug);
:id
和:slug
代表命名参数
我的强烈假设是您只是缺少“ ”以在查询中将 $slug 定义为字符串。 但是使用像上面这样的参数也使它更具可读性。
中查找有关命名或编号参数的说明