Quantcast
Channel: Select One column Doctrine DQL - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by j0k for Select One column Doctrine DQL

$
0
0

This is because Doctrine hydrate the response with all the object information, so all columns.

You need to use a different hydration method, there are many one, but let's focus on 5 of them:

  • HYDRATE_RECORD, the default one
  • HYDRATE_ARRAY
  • HYDRATE_NONE
  • HYDRATE_SCALAR
  • HYDRATE_ARRAY_SHALLOW

You need the HYDRATE_ARRAY_SHALLOW hydration method. Here's why.

  1. HYDRATE_RECORD

    $q = Doctrine_Query::create()    ->select('a.pro_id')    ->from('fndr_proyecto a')    ->where('a.pro_id = ?',1);$pro = $q->execute(array(), Doctrine_Core::HYDRATE_RECORD);var_dump(json_encode($pro->toArray()));

    This will hydrate the result using object, and also hydrate relations (if you use a leftJoin inside your query). Since it returns object, we need to call toArray() to be able to send a propre json:

    [{"id":1,"name":"Project name","year":2013}]"
  2. HYDRATE_ARRAY

    $q = Doctrine_Query::create()    ->select('a.pro_id')    ->from('fndr_proyecto a')    ->where('a.pro_id = ?',1);$pro = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);var_dump(json_encode($pro));

    This will hydrate result as an array an automatically add the primary key:

    [{"id":"1","pro_id":"1"}]"
  3. HYDRATE_NONE

    $q = Doctrine_Query::create()    ->select('a.pro_id')    ->from('fndr_proyecto a')    ->where('a.pro_id = ?',1);$pro = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);var_dump(json_encode($pro));

    This won't hydrate result, and return just values:

    [["1"]]"
  4. HYDRATE_SCALAR

    $q = Doctrine_Query::create()    ->select('a.pro_id')    ->from('fndr_proyecto a')    ->where('a.pro_id = ?',1);$pro = $q->execute(array(), Doctrine_Core::HYDRATE_SCALAR);var_dump(json_encode($pro));

    This will hydrate result from the select but with key index as the column name with the table alias:

    [{"a_pro_id":"1"}]"
  5. HYDRATE_ARRAY_SHALLOW

    $q = Doctrine_Query::create()    ->select('a.pro_id')    ->from('fndr_proyecto a')    ->where('a.pro_id = ?',1);$pro = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY_SHALLOW);var_dump(json_encode($pro));

    This will hydrate result from the select but with key index as the column name without the table alias:

    "[{"pro_id":"1"}]"

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>