Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch only certain fields with query builder

I use doctrine to fetch data from the database. In my repository, I build the query

createQueryBuilder('p')
->select('p, bc, cp')
->innerJoin('p.customer', 'bc')
->innerJoin('bc.person', 'cp')

It works well for me, I get the data. But how can I say to doctrine to give me not all fields, but only certain fields, like p.id, p.name, cp.firstname etc?

Thank's for your answers.

like image 913
olek07 Avatar asked Dec 11 '25 15:12

olek07


1 Answers

$query->createQueryBuilder()
->select('p.id','p.name', 'bc.xyz', 'cp.xyz')
->from('entity','p')
->innerJoin('p.customer', 'bc')
->innerJoin('bc.person', 'cp');

This works for me

like image 172
jack Avatar answered Dec 13 '25 18:12

jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!