Select repository-specific columns in Doctrine 2

Asked

Viewed 255 times

1

First, we know that depending on the number of columns in a query, you can increase the response time.

In Doctrine I call the following repository, which has a relationship and brings all the columns of both entities.

public function index()
{
    $this->alunos = $this->model->getRepository()->findAll();
}

But thinking about the statement I gave earlier, the return of this repository is more time consuming than if it were an entity without relationship?

And another doubt?

Can I select the columns I want to return from this repository? For example, the above repository returns:

id (entidade aluno)
nome (entidade aluno)
id_turma (entidade turma)

But I would like to return only the name of the student. Type like this:

public function index()
{
    $this->alunos = $this->model->getRepository()->findAll("nome");
    // ou assim pra pegar mais de um campo
    $this->alunos2 = $this->model->getRepository()->findAll("nome, dtNascimento");
}

I know it doesn’t work that way, just to illustrate.

2 answers

1

Starting with the second question, you have the possibility to use query Builders

$query = $em->createQuery('SELECT u.id FROM CmsUser u');
$ids = $query->getResult(); // array of CmsUser ids

see here: http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html

On the first question: In that regard I think the performance may vary. If I’m not mistaken, Doctrine treats everything as objects and when you load a certain entity all its dependencies are loaded.

Doctrine does slow down the application by using resources. You’ll have to see what’s most viable for you. Use profile tools to measure the resources used. I think in Zendf and Symfony you have components that measure that.

  • Opa, +Rudi Rocha, thanks for the answer. So, to confirm, there is no way to select these fields by the repository right? Type $this->alunos = $this->model->getRepository()->findAll("nome"); iai would bring only the name field. PS: I know q doesn’t work like this, it was just to exemplify.

  • @Leofelipe The findAll() method does not accept columna names as arguments. I think you should see the documentation at: http://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started.html

1

What you can do is make you return a custom repository to a model specific, rather than returning the EntityReposity standard of the Doctrine.

In this custom repository, which inherits the class EntityRepository, you overwrite the functions so that they return only the columns you want.

Class MyDomain\Model\Aluno:

<?php

namespace MyDomain\Model;

use Doctrine\ORM\EntityRepository;

/**
 * @Entity(repositoryClass="MyDomain\Model\AlunoRepository")
 */
class Aluno
{
}

Class MyDomain\Model\AlunoRepository:

<?php

namespace MyDomain\Model;

class AlunoRepository extends EntityRepository
{
    public function findAll()
    {
        return $this->_em
            ->createQuery('
                SELECT a.coluna1, a.coluna2, a.coluna3
                FROM MyDomain\Model\Aluno a')
            ->getResult();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.