【问题标题】:Symfony/Doctrine - QueryException - [Semantical Error] line 0, col 48 near 'pizza-mia': Error: 'pizza' is not definedSymfony/Doctrine - QueryException - [语义错误] 第 0 行,'pizza-mia' 附近的第 48 列:错误:'pizza' 未定义
【发布时间】:2018-07-25 11:05:57
【问题描述】:

我想使用 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;
}
}

任何帮助将不胜感激。

【问题讨论】:

  • 你能显示你的 Post 类的实体定义吗?为什么 post::class 在您的代码示例中不以大写字母开头?
  • 这是一个错字,我很抱歉。我刚刚添加了 Post 实体类

标签: symfony doctrine-query


【解决方案1】:

试试这个方法。

$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 定义为查询中的字符串。 但使用上述参数也使其更具可读性。

documentation中查找有关命名或编号参数的说明

【讨论】:

  • 它给了我这个错误“无效的参数格式,?给出,但是: or ? 预期。”我尝试将 ' ' 添加到 $slug 但这也不起作用
  • 更新答案 - 您使用的是哪个版本的 Doctrine?
  • 我用的是最新版本,2.6.2
猜你喜欢
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 2021-01-03
  • 1970-01-01
  • 2017-10-20
  • 2023-02-13
  • 2013-08-11
相关资源
最近更新 更多