Select specific columns from a repository in Doctrine 2

浪子不回头ぞ 提交于 2019-12-24 04:44:09

问题


First, we know that depending on the amount of columns in a query, may increase the response time.

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");
}

回答1:


What you want to use is a queryBuilder:

http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html

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


来源:https://stackoverflow.com/questions/27726879/select-specific-columns-from-a-repository-in-doctrine-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!