Doctrine DBAL 2: fetchAll() unnecessary array dimensions

mmmmm picture mmmmm · Sep 20, 2012 · Viewed 22.3k times · Source

In doctrine DBAL2 when I execute a query like this:

<?php
$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);
$stmt->execute(); 

return $stmt->fetchAll();
?>

I get a result like this:

Array
(
    [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:

Array
(
    [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

Docal picture Docal · Apr 16, 2015

You can pass a fetch mode parameter to fetchAll().

$stmt->fetchAll(\PDO::FETCH_COLUMN)