从Doctrine 2中的存储库中选择特定的列 [英] Select specific columns from a repository in Doctrine 2
问题描述
首先,我们知道,取决于查询中的列数,可能会增加响应时间。
First, we know that depending on the amount of columns in a query, may increase the response time.
在Doctrine中,调用以下存储,这具有关系
In Doctrine call the following store, which has a relationship and it brings all the columns of both entities.
public function index()
{
$this->students = $this->model->getRepository()->findAll();
}
但是考虑到我之前给出的声明,该存储库的返回是
But thinking about the statement that I gave earlier, the return of this repository is more time consuming than if it was a non-relationship?
还有其他问题。我可以选择要返回此存储库的列吗?例如,存储库返回上面的内容:
And other questions. Can I select the columns that I want to return this repository? For example, the repository returns above:
id (student organization)
name (student organization)
id_class (class entity)
但是我只想返回学生的名字。例如:
But I would like to return only the student's name. As an example:
public function index()
{
$this->students = $this->model->getRepository()->findAll()->onlyColumns("name");
// Or so to catch more than one column
$this->students = $this->model->getRepository()->findAll()->onlyColumns("name, dateOfBirth");
}
推荐答案
您要使用的是一个queryBuilder:
What you want to use is a queryBuilder:
http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html
执行时->在存储库上的findAll(),它直接进入数据库并获取所有内容(简而言之)。要操纵从数据库中获得的信息,您应该执行以下操作:
when you do ->findAll() on a repository, it goes straight for the database and fetches all(shortly speaking). To manipulate what you get from the database, you shoud do:
$repo->createQueryBuilder()
->select('column1,column2')
->getQuery()
->getResult();
这篇关于从Doctrine 2中的存储库中选择特定的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!