3

I have the following table

Test:
    tableName: test
    columns:
        test_id:
            name: test_id as id
            primary: true
            autoincrement: true
            type: integer
            notnull: true
        test_name:
            name: test_name as name
            type: string(255)
        test_title:
            name: test_title as title
            type: string(255)

and this dql statement

$dql = Doctrine_Query::create()->select('t.name')->from('Model_Test t');

the following sql ist generated

SELECT t.test_id AS t__test_id, t.test_name AS t__test_name FROM test t

but after fetching the result in doctrine, I have access to the title field even it is not selected:

foreach ($results as $result) {
    foreach ($result as $filed => $value) {
        echo "$filed => $value <hr>"; // echoes 'title' => null but title in db has other value
    }                                
}                                

also a dump via Zend_Debug::dump($results->toArray()); shows me all fields as if I would have done a select *

So how to limit the returned fields to match my selection?

Thanks in advance

Martin

Martin
  • 61
  • 1
  • 5

3 Answers3

3

I guess:

because

$results = $dql->execute();

fetches the result as an object, the non selected vars are filled with nulls

whereas

$results = $dql->fetchArray(); 

fetches an array and in this case only the selected fields appear (besides the primary key)

Martin
  • 61
  • 1
  • 5
  • This made a difference for me. You can also pass the parameter `Doctrine_Core::HYDRATE_ARRAY` as the second parameter to `execute` or `fetchOne`, and you'll get the same result as `fetchArray` performs. – Matt Huggins Jan 22 '11 at 21:47
1

This one works for me, and only fetches the queried fields:

$events = Doctrine_Query::create()
     ->select("e.id, e.Title, e.Lat, e.Lon, e.Startdate, e.Location, e.Url")
     ->from('Event e')
     ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
     ->execute();

The resulting array will only contain the selected fields

Attila Fulop
  • 6,861
  • 2
  • 44
  • 50
0

I am not sure, but I think doctrine selects just id and name, but when You try to access title it sees that title is not fetched from DB. So doctrine refetches that object (only this time using SELECT * or similar query).

If You have some kind of Doctrine query profiler -- you probably could see all additional queries required in foreach loop.

Just a wild quess, by the way...

Oh, you can use $query->execute(Doctrine::HYDRATE_ARRAY) if you want to select only some portion of fields.

petraszd
  • 4,249
  • 1
  • 20
  • 12
  • 1
    yes and no: doctrine fetches all fields, but only the selected fields have a value (title appears as null even it has another value in the table). This is confusing: Having the query outsourced in a library the user using the library has no idea of the query. He doesn't know how if title is null because it is not selected, or if title is null in the db. I'm lookink for a way to iterate over the selected fields an only over the selected fields... – Martin Nov 22 '10 at 20:55