Doctrine Same query, getResult vs getArrayResult returning different results

doctrine-orm, mysql, symfony

Solution

In this link: http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html

Query#getResult(): Retrieves a collection of objects. The result is either a plain collection of objects (pure) or an array where the objects are nested in the result rows (mixed).

Query#getArrayResult(): Retrieves an array graph (a nested array) that is largely interchangeable with the object graph generated by Query#getResult() for read-only purposes.

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.

I think something on relation mapping caused this.

Problem

Building the following query: ``` $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(); ``` When calling getResult() (and counting the result set): `$attributes = count($q->getResult());` The result is 1 row returned (which is incorrect). But, when calling getResultArray `$attributes = count($q->getArrayResult());` The result is 2 (which is correct) Table Schema: ``` 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; ``` Expected result: ``` [ { 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" }, } ] ``` Actual Result (when using 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" } ] ``` Actual Result (when using 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" } ] ```

Original source