user4083185
user4083185

Reputation:

Proper way to get an array of values from Doctrine2

I'm currently coding a newsletter system. In order to send the mail, I need to get all e-mail addresses from my database (of course).

So I created a custom repository method, as follows :

public function getEmailAddresses()
{
    $query = $this->getEntityManager()->createQueryBuilder()
        ->select('u.email')
        ->from('AppBundle:User', 'u')
        ->where('u.isNewsletterSubscriber = true')
    ;

    $results =  $query->getQuery()->getResult();

    $addresses = [];
    foreach($results as $line) {
        $addresses[] = $line['email'];
    }

    return $addresses;
}

I am wondering if there is a better way to do so than treating the result to get a "plain" array containing only e-mail addresses. In effect, after $query->getQuery()->getResult(), I get something like this :

'results' =>
 [0] => array('email' => '[email protected]')
 [1] => array('email' => '[email protected]')

And as I said, I want something like this :

array('[email protected]', '[email protected]')

Does a cleaner way to do that exist using Doctrine2 built-in methods ? I've tried with different hydratation modes but nothing worked.

Thanks in advance :)

Upvotes: 3

Views: 1123

Answers (4)

Jason Roman
Jason Roman

Reputation: 8276

You could probably create a custom hydrator, but there's really no issue with just doing it the way you are right now. You could also do it in the following ways:

PHP <= 5.4

return array_map('current', $addresses);

PHP >= 5.5

return array_column($addresses, 'email');

The array_column function was introduced in PHP 5.5.0 and does what you're looking for. The array_map function will work otherwise, calling PHP's internal current function which simply returns the value of the current element (which is always initialized to the first element of that array).

Be careful with using array_map if you have a large number of rows returned, because it will likely be slower and it will definitely take up a lot more memory since it has to copy the array.

Upvotes: 5

Markus
Markus

Reputation: 440

I would rather use the getArrayResult method, so doctrine must not hydrate each object (this is the expensive task from doctrine).

public function getEmailAddresses()
{
    $q = $this->getEntityManager()->createQuery('SELECT u.email FROM AppBundle:User u WHERE u.isNewsletterSubscriber = true');

    return array_map('current', $q->getArrayResult());
}

Upvotes: 0

xurshid29
xurshid29

Reputation: 4210

You can run pure sql with doctrine (DBAL):

example:

public function getEmails()
{
    $connection = $this->getEntityManager()->getConnection()->prepare('SELECT u.email FROM user AS u');

    $connection->execute();

    return $connection->fetchAll(\PDO::FETCH_COLUMN);
}

Upvotes: 1

hazarin
hazarin

Reputation: 21

Try other $hydrationModes, maybe that help

getResult( mixed $hydrationMode = Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT )

Upvotes: 0

Related Questions