I have found solution:

SELECT a, IDENTITY(a.group) AS groupId FROM account a

But for 2 level relations all 'AS' statements are returned at the root 
level.

For example, if we will add 'parentGroup' field to Group, and execute next 
statement:

SELECT 
    a, 
    group,
    IDENTITY(group.parentGroup) AS parentGroupId 
FROM account a
LEFT JOIN a.group group

The result will be:

{
    0: {
        id: '1'
        name: 'Account 1',
        group: {
            id: 1,
            name: 'Group1'
        }
    },
    parentGroupId: 2
}

But expected result is:

{
    id: '1'
    name: 'Account 1',
    group: {
        id: 1,
        name: 'Group1',
        parentGroupId: 2
    }   
}

So, how to get expected result?


среда, 25 февраля 2015 г., 16:34:02 UTC+3 пользователь Pavel Sokolov 
написал:
>
> Hello!
>
> I have Account and Group. Account belongs to Group. (ManyToOne) :
>
> /**
>  * @ORM\Table(name="accounts")
>  * @ORM\Entity
>   */
> class Account {
>     /**
>      * @ORM\Column(name="id", type="integer")
>      * @ORM\Id
>      */
>     protected $id;
>
>     /**
>      * @ORM\Column(name="name", type="string")
>      */
>     protected $name;
>
>     /**
>      * @ORM\ManyToOne(targetEntity="Group")
>      * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
>      */
>     protected $group;
> }
>
>
> /**
>  * @ORM\Table(name="groups")
>  * @ORM\Entity
>  */
> class Group {
>     /**
>      * @ORM\Column(name="id", type="integer")
>      * @ORM\Id
>      */
>     protected $id;
>
>     /**
>      * @ORM\Column(name="name", type="string")
>      */
>     protected $name;
> }
>
>
> My frontend needs to select foreign keys for all ManyToOne associations.
> So, I need to select group_id when I'm doing select with HYDRATE_ARRAY 
> mode.
>
> Next query does not return group_id value:
>
> SELECT a FROM account a
>
> I know that I can declare 'group_id' property for Account model but it 
> does not recommened by Doctrine guidelines:
>
> class Account {
> ...
>     /**
>      * @ORM\Column(name="group_id", type="integer")
>      */
>     protected $groupId;
> ...
> }
>
>
> So, does somebody know any way to select all referenced foreign keys for 
> model (group_id for Account in this example)?
>
> I need something like this, but it does not work:
> SELECT a,a.group_id FROM account a
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to