Doctrine相同查询,getResult vs getArrayResult返回不同的结果

时间:2014-05-11 01:38:06

标签: mysql symfony doctrine-orm

构建以下查询:

$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"
    }
]

1 个答案:

答案 0 :(得分:4)

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

  

Query#getResult():检索对象的集合。结果是   要么是一个简单的对象集合(纯粹的),要么是一个数组   对象嵌套在结果行中(混合)。

     

Query#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