F
F
FasterTans2022-02-02 15:05:11
Doctrine ORM
FasterTans, 2022-02-02 15:05:11

How to select specific columns in relationships?

There are 3 entities with one-to-many relationships (let there be a user, books and book authors). I want to select only the user, an array of books that he reads and the names of the authors of the books. Accordingly, the user, books and the author have a bunch of fields that I don’t need and I don’t want to rake everything from the database.
Actually, if I do this

public function findByUserId($id)
    {
        $query = $this->createQueryBuilder('u');

        $query->select('u,b,a')
            ->leftJoin(
                'u.books',
                'b',
                Join::WITH,
                'b.id = g.userId'
            )
            ->leftJoin(
                'b.authors',
                'a',
                Join::WITH,
                'a.bookId = b.id'
            );

        return $query->getQuery()->getOneOrNullResult();
    }

All fields are given to me, with conditional passwords, dates of publication, birth, death, etc., but I want to get only the user's name, an array of books with book titles and an array with authors' names.
If I select all these fields via select(u.name,b.title,a.name), then I get a bunch of arrays with specific fields, which is a bit different).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question