我有自定义数据类型,它在使用FindBy时按预期工作...但在使用查询构建器时没有。对不起,很长的帖子,但我想更多的信息应该有所帮助。
与这个悬而未决的问题相同: Doctrine 2 Custom Types
...
class MyHappyType extends Type
{
...
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return 'hippies: '.$value;
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return 'doubleHippies: '.$value;
}
...
public function getName()
{
return 'hippies';
}
}
// Entity class
...
class Hippie
{
/**
* @ORM\Id
* @ORM\Column(type="integer", unique=true)
*/
protected $id;
/*
* @ORM\Column(type="hippies")
*/
protected $Sandals;
}
...
class HippiesRepository extends EntityRepository
{
public function useQueryBuilder($sandals){
$qb = $this->createQueryBuilder('hippie');
$qb->select('hippie')
->where('hippie.Sandals = :sandals')
->setParameter('sandals', $sandals);
return $qb->getQuery()->getResult();
}
}
public function hippiesAction()
{
// this returns an entity with $hippie1->sandals == 'doubleHippies: hippies: red'
// which is expected behaviour
$hippie1 = $em->getRepository('HappyHippiesBundle:Hippie')->findOneBySandals('red');
// this one returns no results, when checking queries run I see that
// $sandals value isn't translated in to 'hippies: red'
$hippie2 = $em->getRepository('HappyHippiesBundle:Hippie')->useQueryBuilder('red');
}
简而言之,使用QueryBuilder时,只有在使用FindBy时才会转换数据类型...
答案 0 :(得分:1)
您需要在实体配置中定义存储库类:
/**
* @ORM\Entity(repositoryClass=""AcmeDemoBundle\Entity\HippiesRepository")
*/
class Hippie
{
// ...
}
有关详细信息,请参阅Custom Repository classes。
<强>更新强>
您可能还想摆脱->select('hippie')
。当您使用该方法在存储库类中创建QueryBuilder
时,该方法将自动为您调用select
和from
方法(有关详细信息,请参阅EntityRepository.php文件)。在这种情况下,您需要做的就是:
$qb = $this->createQueryBuilder('hippie');
$qb->where('hippie.Sandals = :sandals')
->setParameter('sandals', $sandals);
return $qb->getQuery()->getResult();