0
votes

I tried to get the last 5 articles but it did not work.

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

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

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

Error:

Fatal error: Uncaught 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 Stack trace:

/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 ...', Array)

/Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(279): Doctrine\ORM\Query\Parser->processDeferredPathExpressions(Object(Doctrine\ORM\Query\AST\SelectStatement)) /Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.p in /Users/guillaumedouceron/Desktop/web-dev/iim-promo2021-a2-oop-project/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php on line 63

Entity Article:

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;
}
}

Can someone help me? please

1
I quess the semicolon at the end of "->orderBy('a.created_at', 'DESC');" is a typo ?Jibato
It's just a copy error ;) I edited the code.GuiguiWeb
Can you provide the code of Article entity?Ahmed TAILOULOUTE
@AhmedTAILOULOUTE Done !GuiguiWeb

1 Answers

1
votes

You should try this :

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

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

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