Query Builder in repository: Error: Class 'AppBundle\Entity\' is not defined

Query Builder in repository: Error: Class 'AppBundle\Entity\' is not defined

我尝试通过自定义存储库创建自定义删除逻辑:

namespace AppBundle\Repository;

use AppBundle\Entity\ContactEmail;

class ContactEmailRepository extends \Doctrine\ORM\EntityRepository
{
  /**
  * Remove an email from the database
  * @param String $email
  */
  public function deleteEmail($email)
  {
    $em=$this->getEntityManager();

    $queryBuilder = $em->createQueryBuilder();
    $queryBuilder->delete('AppBundle::ContactEmail','c')
      ->where('c.email=:email')
      ->setParameter('email',$email);

    $query=$queryBuilder->getQuery();

    $p = $query->execute();

    return $p;
  }
}

我通过功能测试对其进行了测试:

namespace Tests\AppBundle\Repository;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use AppBundle\Entity\ContactEmail;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\Tools\SchemaTool;
Use Doctrine\Common\DataFixtures\Purger\ORMPurger;


use AppBundle\DataFixtures\ContactEmailDataFixture;



class ContactEmailTest extends KernelTestCase
{
/**
    * @var \Doctrine\ORM\EntityManager
    */
   private $entityManager;

   /**
    * {@inheritDoc}
    */
   protected function setUp()
   {
       $kernel = self::bootKernel();

       $this->entityManager = $kernel->getContainer()
           ->get('doctrine')
           ->getManager();

       //In case leftover entries exist
       $schemaTool = new SchemaTool($this->entityManager);
       $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();

       // Drop and recreate tables for all entities
       $schemaTool->dropSchema($metadata);
       $schemaTool->createSchema($metadata);
   }

   /**
   * Testing whether a preloaded email will get deleted
   */
   public function testDeletion()
   {
     $fixture = new ContactEmailDataFixture();
     $fixture->load($this->entityManager);

     /**
     * @var Appbundle\Repository\ContactEmailRepository
     */
     $repository=$this->entityManager->getRepository(ContactEmail::class);

     $emailToDelete='jdoe@example.com';
     $repository->deleteEmail($emailToDelete);

     $emailSearched=$repository->findOneBy(['email'=>$emailToDelete]);
     $this->assertEmpty($emailSearched);
   }
}

我的实体如下:

namespace AppBundle\Entity;

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

/**
 * ContactEmail
 *
 * @ORM\Table(name="contact_email")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ContactEmailRepository")
 * @UniqueEntity("email")
 */
class ContactEmail
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
    * @var String
    * @ORM\Column(name="email",type="string",unique=true)
    * @Assert\Email( message = "The email '{{ value }}' is not a valid email.")
    */
    private $email;

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

    public function __construct()
    {
      $this->createdAt=new \DateTime("now", new \DateTimeZone("UTC"));
    }

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

    /**
    * @param String $email
    * @return ContactEmail
    */
    public function setEmail($email){
      $this->email=$email;

      return $this;
    }

    /**
    * @return String
    */
    public function getEmail(){
      return $this->email;
    }

    public function getCreatedAt()
    {
      return $this->createdAt;
    }
}

我使用以下 DataFixture:

namespace AppBundle\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use AppBundle\Entity\ContactEmail;

class ContactEmailDataFixture extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $emails=['jdoe@example.com','example@gmail.com','user1@example.com'];
        foreach($emails as $email){
          $emailEntityInstance=new ContactEmail();
          $emailEntityInstance->setEmail($email);

          $manager->persist($emailEntityInstance);
          $manager->flush();
        }
    }
}

问题是我 运行 测试时出现以下错误:

Doctrine\ORM\Query\QueryException: [Semantical Error] line 0, col 7 near 'AppBundle::ContactEmail': Error: Class 'AppBundle\Entity\' is not defined.

所以我想知道我在查询语法上做错了什么?

在扩展 Doctrine\ORM\EntityRepository 并且在实体的 @Entity 文档块注释的 repositoryClass 属性中引用的 class 中,您可以获得这样的查询构建器:

$queryBuilder = $this->createQueryBuilder('c')
  ->where('c.email=:email')
  ->setParameter('email',$email);