0
votes

I've got two entity managers, each for a different database, with distinct doctrine mappings, entities and repositories. I'm trying to use them from within the context of a Symfony2 ContainerAwareCommand:

$this->em = $this->getContainer()->get('doctrine')->getManager('default');
$this->cid = $this->getContainer()->get('doctrine')->getManager('cid');

The default one works as expected:

$users = $this->em->createQueryBuilder()
    ->select('u')
    ->from('VdwSwimBundle:User', 'u')
    ->getQuery()
    ->getResult(Query::HYDRATE_OBJECT)
;
// $users ==> array(User1, User2, ...UserN)

However the second one returns the correct number of entities, but they're all the same:

$users = $this->cid->createQueryBuilder()
    ->select('u')
    ->from('VdwCidBundle:CidUser', 'u')
    ->getQuery()
    ->getResult(Query::HYDRATE_OBJECT)
;
// $users ==> array(UserN, UserN, ...UserN)

Meanwhile HYDRATE_ARRAY returns distinct user-data as one would expect:

$users = $this->cid->createQueryBuilder()
    ->select('u')
    ->from('VdwCidBundle:CidUser', 'u')
    ->getQuery()
    ->getResult(Query::HYDRATE_ARRAY)
;
// $users ==> array(array1, array2, ...arrayN)

Any idea what gives here?

For reference, provided below are the config, mapping definition and entity class:

app/config/config.yml:

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8
            cid:
                driver:   %cid_database_driver%
                host:     %cid_database_host%
                port:     %cid_database_port%
                dbname:   %cid_database_name%
                user:     %cid_database_user%
                password: %cid_database_password%
                charset:  UTF8

    orm:
        auto_generate_proxy_classes: true
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    VdwSwimBundle: ~
            cid:
                connection: cid
                mappings:
                    VdwCidBundle: ~

CidUser mapping:

<!-- src/Vdw/CidBundle/Resources/config/doctrine/CidUser.orm.xml -->
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping
        xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                            http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Vdw\CidBundle\Entity\CidUser" table="cid_user"
          repository-class="Vdw\CidBundle\Repository\CidUserRepository">
    <id name="id" type="boolean" column="cid_user_id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="name" type="string" column="name" length="255" nullable="false"/>
    <field name="password" type="string" column="pass" length="255" nullable="false"/>
    <field name="perms" type="boolean" column="perms" nullable="false"/>
    <field name="clientIds" type="text" column="cid_client_ids" nullable="false"/>
  </entity>
</doctrine-mapping>

And the entity class:

// src/Vdw/CidBundle/Entity/CidUser.php
<?php

namespace Vdw\CidBundle\Entity;

/**
 * CidUser
 */
class CidUser
{
    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $password;

    /**
     * @var boolean
     */
    private $perms;

    /**
     * @var string
     */
    private $clientIds;

    /**
     * @var boolean
     */
    private $id;


    /**
     * Set name
     *
     * @param string $name
     * @return CidUser
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return CidUser
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set perms
     *
     * @param boolean $perms
     * @return CidUser
     */
    public function setPerms($perms)
    {
        $this->perms = $perms;

        return $this;
    }

    /**
     * Get perms
     *
     * @return boolean
     */
    public function getPerms()
    {
        return $this->perms;
    }

    /**
     * Set clientIds
     *
     * @param string $clientIds
     * @return CidUser
     */
    public function setClientIds($clientIds)
    {
        $this->clientIds = $clientIds;

        return $this;
    }

    /**
     * Get clientIds
     *
     * @return string
     */
    public function getClientIds()
    {
        return $this->clientIds;
    }

    /**
     * Get id
     *
     * @return boolean
     */
    public function getId()
    {
        return $this->id;
    }
}
1

1 Answers

0
votes

Try to remove the parameter Query::HYDRATE_OBJECT , As by default Doctrine hydrates your user record to objects. the ->getResult() method will return an array of User object.

EDIT

Check the correct type of your user Id in your orm.xml file (boolean)

<id name="id" type="boolean" column="cid_user_id">
  <generator strategy="IDENTITY"/>
</id>