问题
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