我有两个实体管理器,每个管理器用于不同的数据库,具有不同的学说映射,实体和存储库。我正在尝试在Symfony2 ContainerAwareCommand的上下文中使用它们:
$this->em = $this->getContainer()->get('doctrine')->getManager('default');
$this->cid = $this->getContainer()->get('doctrine')->getManager('cid');
默认值按预期工作:
$users = $this->em->createQueryBuilder()
->select('u')
->from('VdwSwimBundle:User', 'u')
->getQuery()
->getResult(Query::HYDRATE_OBJECT)
;
// $users ==> array(User1, User2, ...UserN)
然而,第二个实体返回正确数量的实体,但它们都是相同的:
$users = $this->cid->createQueryBuilder()
->select('u')
->from('VdwCidBundle:CidUser', 'u')
->getQuery()
->getResult(Query::HYDRATE_OBJECT)
;
// $users ==> array(UserN, UserN, ...UserN)
与此同时,HYDRATE_ARRAY会按照预期返回不同的用户数据:
$users = $this->cid->createQueryBuilder()
->select('u')
->from('VdwCidBundle:CidUser', 'u')
->getQuery()
->getResult(Query::HYDRATE_ARRAY)
;
// $users ==> array(array1, array2, ...arrayN)
知道这里给出了什么吗?
供参考,下面提供了配置,映射定义和实体类:
应用程序/配置/ 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>
实体类:
// 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;
}
}
答案 0 :(得分:0)
尝试删除参数Query::HYDRATE_OBJECT
,
默认情况下,Doctrine会将您的用户记录保存到对象。
->getResult()
方法将返回User对象数组。
编辑
在orm.xml文件中检查用户ID的正确类型(布尔值)
<id name="id" type="boolean" column="cid_user_id">
<generator strategy="IDENTITY"/>
</id>