2014-05-11 55 views
0

号楼下面的查询:主义相同的查询的getResult VS getArrayResult返回不同的结果

$q = $this 
    ->createQueryBuilder('a') 
    ->select('a') 
    ->where('a.account = :accountId') 
    ->andWhere('a.organization = :organization_id') 
    ->setParameters(
     array(
      'accountId' => $account_id, 
      'organization_id' => $organization_id, 
     ) 
    ) 
    ->getQuery(); 

当调用的getResult()(计数结果集):$attributes = count($q->getResult());结果返回1行(这是不正确的) 。

但是,主叫getResultArray $attributes = count($q->getArrayResult());当结果是2(这是正确的)

表结构:

CREATE TABLE 'accountattribute' (
    'account_id' int(11) NOT NULL, 
    'organization_id' int(11) NOT NULL, 
    'dataKey' varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '', 
    'dataValue' text COLLATE utf8_unicode_ci NOT NULL, 
    'updated' datetime NOT NULL, 
    'created' datetime NOT NULL, 
    PRIMARY KEY ('account_id','organization_id','dataKey'), 
    KEY 'organization_id' ('organization_id'), 
    CONSTRAINT 'accountattribute_ibfk_1' FOREIGN KEY ('account_id') REFERENCES 'accounts' ('id'), 
    CONSTRAINT 'accountattribute_ibfk_2' FOREIGN KEY ('organization_id') REFERENCES 'organizations' ('id') 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

预期结果:

[ 
    { 
     data_key: "foo", 
     data_key: "bar", 
     updated: "2014-05-10T21:09:56+0000", 
     created: "2014-05-10T21:09:56+0000", 
     account: { 
      id: 1, 
      display_name: "Test Account", 
      organization: { 
       id: 1, 
       display_name: "NAME" 
      }, 
      active: true, 
      deleted: false, 
      updated: "2014-05-09T15:25:16+0000", 
      created: "2014-05-09T15:25:16+0000" 
     }, 
     organization: { 
      id: 1, 
      display_name: "NAME" 
     }, 
    }, 
    { 
     data_key: "nice", 
     data_key: "ace", 
     updated: "2014-05-11T01:04:43+0000", 
     created: "2014-05-11T01:04:43+0000", 
     account: { 
      id: 1, 
      display_name: "Test Account", 
      organization: { 
       id: 1, 
       display_name: "NAME" 
      }, 
      active: true, 
      deleted: false, 
      updated: "2014-05-09T15:25:16+0000", 
      created: "2014-05-09T15:25:16+0000" 
     }, 
     organization: { 
      id: 1, 
      display_name: "NAME" 
     }, 
    } 
] 

实际结果(使用getResult时)

[ 
    { 
     account: { 
      id: 1, 
      display_name: "Test Account", 
      organization: { 
       id: 1, 
       display_name: "NAME" 
      }, 
      active: true, 
      deleted: false, 
      updated: "2014-05-09T15:25:16+0000", 
      created: "2014-05-09T15:25:16+0000" 
     }, 
     organization: { 
      id: 1, 
      display_name: "NAME" 
     }, 
     data_key: "foo", 
     data_value: "bar", 
     updated: "2014-05-10T21:09:56+0000", 
     created: "2014-05-10T21:09:56+0000" 
    } 
] 

实际结果(使用getArrayResult时)

[ 
    { 
     account: { 
      id: 1, 
      display_name: "Test Account", 
      organization: { 
       id: 1, 
       display_name: "NAME" 
      }, 
      active: true, 
      deleted: false, 
      updated: "2014-05-09T15:25:16+0000", 
      created: "2014-05-09T15:25:16+0000" 
     }, 
     organization: { 
      id: 1, 
      display_name: "NAME" 
     }, 
     data_key: "foo", 
     data_value: "bar", 
     updated: "2014-05-10T21:09:56+0000", 
     created: "2014-05-10T21:09:56+0000" 
    } 
] 
+0

你能显示你的实体的映射吗? –

+0

您是否手动在数据库中添加数据? 'getArrayResult'不会保护你的实体,所以它可能不会验证你的数据的完整性。 –

+0

@Thomas是的,我手动添加了数据。当使用'getResult'时,结果会以正确的值进行水合,但只返回第一个结果。而当我使用'getArrayResult'时,两个结果都会返回。问题是'getResult'没有像期望的那样返回两个结果。 – mikeluby

回答

3

在此链接:http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html

查询#的getResult():检索对象的集合。结果为 对象的纯集合(纯)或对象嵌套在结果行(混合)中的数组。

查询#getArrayResult():获取的阵列图形(嵌套阵列),该 与由 查询#的getResult()用于只读目的生成的对象图很大程度上互换。

An array graph can differ from the corresponding object graph in certain scenarios due to the difference of the identity semantics between arrays and objects. 

我认为关系映射引起了这一点。

更新1: 我发现,解决类似的问题。请检查它:

Doctrine Query getARrayResult

+0

下面是实体及其映射:https://gist.github.com/mikeluby/1684174d1aa9c6451d74 – mikeluby

+0

我还包括存储库。 – mikeluby

相关问题