存储库中的查询生成器:错误:未定义类 'AppBundleEntity'



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

namespace AppBundleRepository;
use AppBundleEntityContactEmail;
class ContactEmailRepository extends DoctrineORMEntityRepository
{
/**
* 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 TestsAppBundleRepository;
use SymfonyBundleFrameworkBundleTestKernelTestCase;
use AppBundleEntityContactEmail;
use DoctrineDBALExceptionUniqueConstraintViolationException;
use DoctrineORMToolsSchemaTool;
Use DoctrineCommonDataFixturesPurgerORMPurger;

use AppBundleDataFixturesContactEmailDataFixture;

class ContactEmailTest extends KernelTestCase
{
/**
* @var DoctrineORMEntityManager
*/
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 AppbundleRepositoryContactEmailRepository
*/
$repository=$this->entityManager->getRepository(ContactEmail::class);
$emailToDelete='jdoe@example.com';
$repository->deleteEmail($emailToDelete);
$emailSearched=$repository->findOneBy(['email'=>$emailToDelete]);
$this->assertEmpty($emailSearched);
}
}

我的实体如下:

namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* ContactEmail
*
* @ORMTable(name="contact_email")
* @ORMEntity(repositoryClass="AppBundleRepositoryContactEmailRepository")
* @UniqueEntity("email")
*/
class ContactEmail
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var String
* @ORMColumn(name="email",type="string",unique=true)
* @AssertEmail( message = "The email '{{ value }}' is not a valid email.")
*/
private $email;
/**
* @ORMColumn(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;
}
}

我使用以下数据夹具:

namespace AppBundleDataFixtures;
use DoctrineBundleFixturesBundleFixture;
use DoctrineCommonPersistenceObjectManager;
use AppBundleEntityContactEmail;
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();
}
}
}

问题是我在运行测试时收到以下错误:

DoctrineORMQueryQueryException: [Semantical Error] line 0, col 7 near 'AppBundle::ContactEmail': Error: Class 'AppBundleEntity' is not defined.

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

在扩展DoctrineORMEntityRepository并在实体的@Entitydocblock 注释的repositoryClass属性中引用的类中,您可以获得如下所示的查询生成器:

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

相关内容

  • 没有找到相关文章

最新更新