7
我有自定義數據類型,它在按照預期使用FindBy時工作...但在使用查詢生成器時不起作用。對不起,很長的文章,但我想更多的信息應該有所幫助。Symfony Doctrine數據類型只適用於findBy而不是querybuilder
它是相同的,因爲這沒有答案的問題: 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時...
再考慮一下,使用查詢構建器時不會考慮自定義類型。它們連接到實體,它與查詢無關 - 這只是一個查詢。 所以我想問題是:你如何在自定義類型的倉庫中使用方法? – offwhite 2014-09-18 08:34:20