小编sim*_*lay的帖子

Symfony4 : 注释不存在,或无法自动加载 (Symfony\Component\Validator\Constraints)

我正在开发 Symfony4 应用程序,但出现此错误:

[语义错误] 属性 App\Entity\Product::$brochure 中的注释“@Symfony\Component\Validator\Constraints\NotBlank”不存在,或无法自动加载。

这是Product课程:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;
    
    /**
     * @ORM\Column(type="decimal", scale=2, nullable=true)
     */
    private $price;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\Column(type="string")
     *
     * @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
     * @Assert\File(mimeTypes={ "application/pdf" })
     */
    private $brochure;
    
    public …
Run Code Online (Sandbox Code Playgroud)

php annotations symfony symfony-validator symfony4

3
推荐指数
1
解决办法
5584
查看次数

Symfony4:实体将repositoryClass设置为"App\Entity\CommentRepository",但这不是有效的类

在Symfony4中我有一个存储库:

<?php

namespace App\Repository;

use App\Entity\Comment;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

class CommentRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Comment::class);
    }

    public function findByNews($news)
    {
        return $this->createQueryBuilder('c')
            ->where('c.news = :news')
            ->setParameter('news', $news)
            //->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在我的ajax控制器的这个动作中使用它时:

public function comments(Request $request)
    {
        if ($request->isXmlHttpRequest()) {
            $newsId = $request->get('id');

            $newsRepository = $this->getDoctrine()->getRepository(News::class);
            $news = $newsRepository->find($newsId);
            $commentRepository = $this->getDoctrine()->getRepository(Comment::class);
            $comments = $commentRepository->findByNews($news);

            $comments = 0;

            $serializer = $this->get('serializer');
            $response = $serializer->serialize($comments, 'json');

            return new JsonResponse(array('data' => $response));
        } …
Run Code Online (Sandbox Code Playgroud)

php repository symfony doctrine-orm

2
推荐指数
1
解决办法
6424
查看次数