Symfony 表单 query_buider 和实体存储库 [英] Symfony form query_buider and entity repository

查看:20
本文介绍了Symfony 表单 query_buider 和实体存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据登录的用户创建一个包含集合类型数据的表单.我正在关注 Symfony 食谱的这一章.

I'm trying to create a form with data in collection type depending on the user being logged. I'm following this chapter of the Symfony cookbook.

query_builder 选项是我从 DQL 获取数据的闭包时,一切正常.由于需要从代码中的不同位置获取数据,我更愿意在 Repository 类中定义查询.

Everything works fine when the query_builder option is a closure where I get my data from DQL. As the data need to be fetched from different location in code, I would prefer to define the query in the Repository class.

这是我存储库中的函数:

Here is the function in my repository :

public function findOwnedBy($user) {
    $query = $this->getEntityManager()->createQuery("SELECT l FROM MyBundle:Article a JOIN a.owndBy u WHERE u.id = :userId");
    $query->setParameters(array("userId"=>$user->getId()));
    return $query->getResult();
}

这个函数在控制器中调用时起作用并返回一个文章数组.这是 symfony 文档的片段:

This function works when called in a Controller and return an array of Article. Here is a snippet of the symfony doc :

$formOptions = array(
                    'class' => 'AcmeDemoBundleEntityUser',
                    'multiple' => false,
                    'expanded' => false,
                    'property' => 'fullName',
                    'query_builder' => function(EntityRepository $er) use ($user) {
                        // build a custom query, or call a method on your repository (even better!)
                    },
                );

当我在 query_builder 中调用我的 Repository 函数时,出现错误:Expected argument of type "DoctrineORMQueryBuilder", "array" given,我可以理解,因为我的 Repository 返回一个实体数组,而不是一个 QueryBuilder.

When I put a call to my Repository function in the query_builder, I get an error : Expected argument of type "DoctrineORMQueryBuilder", "array" given, which I can understand because my Repository returns an array of Entity, not a QueryBuilder.

我不想复制代码并在表单中创建一个新的 QueryBuilder.使用 Repository 中的查询的最佳实践是什么?我想在存储库中有两个函数,一个返回数组,另一个返回 QueryBuilder,但是 Symfony 文档中的注释或调用存储库上的方法(甚至更好!)"让我认为这种情况有更好的方法.

I don't want to duplicate code and create a new QueryBuilder in the Form. What is the best practice to use the query from the Repository ? I was thinking of having two function in the repository, one returning an array and the other returning the QueryBuilder, but the comment in Symfony doc "or call a method on your repository (even better!)" let me think there's better way for this case.

推荐答案

应该很容易.执行以下操作:

It should be easy. Do the following:

public function queryOwnedBy($user) {

    $query = $this->createQueryBuilder('a')
            ->from('MyBundle:Article', 'a')
            ->innerJoin('a.owndBy', 'u')
            ->where('u.id = :id')                
            ->setParameter('id', $user->getId());

    return $query;
}

public function findOwnedBy($user) {
    return $this->queryOwnedBy($user)
            ->getQuery()
            ->getResult();
}

然后在表单生成器中:

$formOptions = array(
    'class' => 'AcmeDemoBundleEntityUser',
    'multiple' => false,
    'expanded' => false,
    'property' => 'fullName',
    'query_builder' => function(EntityRepository $er) use ($user) {
        return $er->queryOwnedBy($user);
    },
);

编辑

感谢 ncatnow 和 unagi 我已经更改了以前的函数以返回查询构建器

Thank's for ncatnow and unagi I've changed the previous functions to return the querybuilder

这篇关于Symfony 表单 query_buider 和实体存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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