存储库中的查询生成器:错误:未定义类“ AppBundle \ Entity \”

时间:2018-08-18 12:25:51

标签: symfony doctrine-orm dql symfony-3.4

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

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.

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

1 个答案:

答案 0 :(得分:1)

在扩展Doctrine\ORM\EntityRepository并在实体的@Entity docblock注释的{​​{1}}属性中引用的类中,您可以获取如下查询构建器:

repositoryClass