Yolanda van der Vegt Yolanda van der Vegt - 1 year ago 147
PHP Question

Doctrine DBAL 2: fetchAll() unnecessary array dimensions

In doctrine DBAL2 when I execute a query like this:

$connection = $this->getDatabaseConnection();

$sql = "SELECT page_url
FROM cms_user_page
WHERE site_id = :siteid
AND active = '1'

$stmt = $connection->prepare($sql);
$stmt->bindValue("siteid", $id);

return $stmt->fetchAll();

I get a result like this:

[0] => Array
[page_url] => index.php?action=login

[1] => Array
[page_url] => index.php?action=shoppingcart

[2] => Array
[page_url] => index.php?action=products

My question is, is there a fetch mode that produces an result like this:

[0] => index.php?action=login

[1] => index.php?action=shoppingcart

[2] => index.php?action=products

I could not find any info about fetch modes in the documentation. and i could do an array map. But thats overhead in my opinion..

Answer Source

If you have more than one case where you need that form of result, although i dont really understand the sense too, you could implement AbstractHydrator interface to create your own ArrayHydrator that returns the structure as you need it.

Hydration Classes reside in NS:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download