2010-11-22 65 views
3

我下表学说select语句总是返回表中的所有字段

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) 

这DQL声明

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

以下SQL IST产生

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

但毕竟有在原则中获取结果,即使没有选择,我也可以访问标题字段:

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

也通过Zend_Debug :: dump($ results-> toArray())转储;显示我所有的领域,如果我会做一个选择*

那么如何限制返回的字段以匹配我的选择?

在此先感谢

马丁

+0

你有没有发现这种情况的解决方案?我也在努力。我不想将不必要的垃圾作为json传递给ajax消费者。 – 2012-04-01 11:18:20

+0

Nevermind, - > setHydrationMode(Doctrine :: HYDRATE_ARRAY)已解决它。 – 2012-04-01 11:29:05

回答

0

我不知道,但我想学说仅选择idname,但是当您尝试访问title它看到title不是从数据库中取出。所以教义重新提到这个对象(只有这次使用SELECT *或类似的查询)。

如果您有某种Doctrine查询分析器 - 您可能会看到foreach循环中需要的所有附加查询。

只是胡乱quess,顺便......

哦,你可以,如果你想选择字段的只有一些部分使用$query->execute(Doctrine::HYDRATE_ARRAY)

+1

yes和no:doctrine提取所有字段,但只有选定的字段有一个值(标题显示为null,即使它在表中还有其他值)。这很让人困惑:将查询外包到库中,使用库的用户不知道查询。他不知道如果标题为空,因为它没有被选中,或者如果标题在数据库中为空。我正在寻找一种方法来遍历选定的字段,只在选定的字段上... – Martin 2010-11-22 20:55:36

3

我想:

因为

$results = $dql->execute(); 

取该结果作为一个对象,所述非选择的瓦尔被用空值填充

$results = $dql->fetchArray(); 

获取的阵列和在这种情况下,只有选定的字段出现(除了主键)

1

这一个对我的作品,只取查询的字段:

$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(); 

结果数组将只包含选定字段