Q:

Return selected columns from a query in Doctrine 2/ZF

I am using Doctrine 2 to manage my entity and Zend_Auth to manage authorisation. I can easily get a full details of a user using
$user = $this->em->getRepository('Entities\User')->findOneByUsername($username));

I successfully authorise, and then want to store the user so that I can access certain details
$storage = $auth->getStorage();
$storage->write($user);

This is all working fine, but I am concerned about storing the complete user details into Zend_Auth storage. 
My thought was to limit the columns returned by findOneByUsername (removing password, and other results). Reading the Doctrine documentation, this does not seem to be possible. I have also looked at queryBuilder (and can build a suitable query), but once again seem to only be able to SELECT all.
What is the best way to limit a query result to a few column?

A:

Storing a user object in the session is not a good practice. You should just store the user id and query the database if needed and store the user object in an instance variable. There are a lot of example here how to write a custom select. Note that the type given back by $query->getResult() depends on the select statement... if you don't select all fields an array will be returned.

