Symfony Doctrine数据类型仅适用于findBy而不是querybuilder [英] Symfony Doctrine datatype only works in findBy not querybuilder

查看:205
本文介绍了Symfony Doctrine数据类型仅适用于findBy而不是querybuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义数据类型,它在使用FindBy ...时工作正常,但不使用查询构建器时。对不起,长篇文章,但我想更多的信息应该有帮助。

I have custom Datatype, which is working as expected when using FindBy... but doesn't when using query builder. Sorry for the long post, but I figure more info should help.

它与这个未回答的问题是一样的:
原则2自定义类型

it's the same as this unanswered question: 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:

// Entity class

...
class Hippie
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer", unique=true)
     */
     protected $id;

   /*
    * @ORM\Column(type="hippies")
    */
    protected $Sandals;

}



资料库:



Repository:

...
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();
    }
}



最后,Controller:



And finally, Controller:

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 ...

So in short, Datatypes aren't being converted when using QueryBuilder, only when using FindBy...

推荐答案

您需要在实体配置中定义存储库类:

You need to define the repository class in your entity configuration:

/**
 * @ORM\Entity(repositoryClass=""AcmeDemoBundle\Entity\HippiesRepository")
 */
class Hippie
{ 
    // ...
}



有关详细信息,请参阅自定义存储库类

更新:

您可能还想摆脱 - > select('hippie')当您使用该方法在存储库类中创建一个 QueryBuilder 时,方法将自动调用选择和方法(请参阅 EntityRepository.php 文件了解更多信息)。在这种情况下,您需要做的是:

You may also want to get rid of the ->select('hippie'). When you use the method to create a QueryBuilder in the repository class, the method will automatically call the select and from methods for you (See the EntityRepository.php file for more info). In this case all you need to do is:

 $qb = $this->createQueryBuilder('hippie');
 $qb->where('hippie.Sandals = :sandals')
    ->setParameter('sandals', $sandals);

 return $qb->getQuery()->getResult();

这篇关于Symfony Doctrine数据类型仅适用于findBy而不是querybuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆