致命错误:未捕获的Doctrine \ ORM \ Query \ QueryException

时间:2017-12-17 22:05:32

标签: php symfony doctrine-orm orm doctrine

我试着收到最后5篇文章,但它没有用。

public function loadLast()
{
    $queryBuilder = $this->createQueryBuilder('a');

    $queryBuilder
        ->orderBy('a.created_at', 'DESC')
        ->setMaxResults(5);

    return $queryBuilder->getQuery()->execute();
}

错误:

  

致命错误:未捕获的Doctrine \ ORM \ Query \ QueryException:SELECT a FROM   App \ Entity \ Article a ORDER BY a.created_at DESC in   /Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:41   堆栈跟踪:

     

/Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(483):   Doctrine \ ORM \ Query \ QueryException :: dqlError('SELECT a FROM A ...')

     

/Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(758):   Doctrine \ ORM \ Query \ Parser-> semanticalError('line 0,col 46 ...',   数组)

     

/Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(279):   教义\ ORM \查询\ Parser-> processDeferredPathExpressions(对象(学说\ ORM \查询\ AST \ SelectStatement))   /Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.p   在   第63行/Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php

实体文章:     

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Cocur\Slugify\Slugify;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
 * @ORM\Table(name="article")
 */
class Article
{
/**
 * @var int
 *
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(type="string")
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(type="string")
 */
private $slug;

/**
 * @var string
 *
 * @ORM\Column(type="text")
 */
private $content;

/**
 * @var string
 *
 * @ORM\Column(type="text")
 */
private $excerpt;

/**
 * @var string
 *
 * @ORM\Column(type="string")
 */
private $image;

/**
 * @var int
 *
 * @ORM\Column(type="smallint")
 */
private $status;

/**
 * @var User
 *
 * @ORM\ManyToOne(targetEntity="User")
 */
private $author;

/**
 * @var \DateTime
 *
 * @ORM\Column(type="datetime", name="created_at")
 */
private $createdAt;

const STATUS_PUBLISHED   = 0;
const STATUS_UNPUBLISHED = 1;
const STATUS_DRAFT       = 2;
const MAX_PER_PAGE       = 6;

/**
 * @return int
 */
public function getId(): ?int
{
    return $this->id;
}

/**
 * @param int $id
 *
 * @return Article
 *
 * @throws \Exception
 */
public function setId(int $id): Article
{
    // Throwable
    // -> Error
    // -> Exception
    if ($id < 1) {
        throw new \Exception("Invalid value, id must be >= 1");
    }

    $this->id = $id;

    return $this;
}

/**
 * @return string
 */
public function getName(): ?string
{
    return $this->name;
}

/**
 * @param string $name
 *
 * @return Article
 *
 * @throws \Exception
 */
public function setName(string $name): Article
{
    if (empty($name)) {
        throw new \Exception("Article name cannot be empty.");
    }

    $this->setSlug($name);

    $this->name = $name;

    return $this;
}

/**
 * @return string
 */
public function getSlug(): string
{
    return $this->slug;
}

/**
 * @param string $slug
 *
 * @return Article
 */
public function setSlug(string $slug) : Article
{
    // $this->slug = $container->get('slugify')->slugify($slug);

    $slugify = new Slugify();
    $this->slug = $slugify->slugify($slug);

    return $this;
}

/**
 * @return string
 */
public function getContent(): ?string
{
    return $this->content;
}

/**
 * @param string $content
 *
 * @return Article
 */
public function setContent(string $content): Article
{
    $this->content = $content;

    return $this;
}

/**
 * @return string
 */
public function getExcerpt(): ?string
{
    return $this->excerpt;
}

/**
 * @param string $excerpt
 *
 * @return Article
 */
public function setExcerpt(string $excerpt): Article
{
    $this->excerpt = $excerpt;

    return $this;
}

/**
 * @return string
 */
public function getImage(): string
{
    return $this->image;
}

/**
 * @param string $image
 *
 * @return Article
 */
public function setImage(string $image): Article
{
    $this->image = $image;

    return $this;
}

/**
 * @return int
 */
public function getStatus(): ?int
{
    return $this->status;
}

/**
 * @param int $status
 *
 * @return Article
 *
 * @throws \Exception
 */
public function setStatus(int $status): Article
{
    if (!in_array($status, self::getStatuses())) {
        throw new \Exception("Status value not valid");
    }

    $this->status = $status;

    return $this;
}

public static function getStatuses()
{
    return [
        self::STATUS_DRAFT,
        self::STATUS_UNPUBLISHED,
        self::STATUS_PUBLISHED,
    ];
}

/**
 * @return User
 */
public function getAuthor(): ?User
{
    return $this->author;
}

/**
 * @param User $author
 *
 * @return Article
 */
public function setAuthor(User $author): Article
{
    $this->author = $author;

    return $this;
}

/**
 * @return \DateTime
 */
public function getCreatedAt(): \DateTime
{
    return $this->createdAt;
}

/**
 * @param \DateTime $createdAt
 * @return Article
 */
public function setCreatedAt(\DateTime $createdAt): Article
{
    $this->createdAt = $createdAt;

    return $this;
}
}

有人能帮助我吗?请

1 个答案:

答案 0 :(得分:1)

你应该试试这个:

public function loadLast()
{
    $queryBuilder = $this->createQueryBuilder('a');

    $queryBuilder
        ->orderBy('a.createdAt', 'DESC')
        ->setMaxResults(5);

    return $queryBuilder->getQuery()->getResult();
}