我有这样的dql:
$qb = $this->em->createQueryBuilder();
$qb
->select('c.login, c.char_name,c.id,c.sex,c.pvpkills,c.pkkills,c.online,c.online_time,c.last_access,MAX(cs.level) as lvl,sub.name,sub.clan_id')
->from($this->target,'c')
->leftJoin('CharacterSubclass','cs','WITH','c.id = cs.char_id')
->leftJoin('clan','cln','WITH','c.clan_id = cln.id')
->leftJoin('ClanSub','sub','WITH','cln.id = sub.clan_id')
->where('c.clan_id = :id')
->setParameter('id',$id)
->groupBy('c.id')->orderBy('lvl','DESC');
$rs = $qb->getQuery()->getResult();
结果不在对象形式中。
array(100)=>{[0]=>array(8){['id']=>'axb' [name]=>'fyz'} [1]=>array{....}}
我尝试了结果映射,但没有希望,浪费时间。无论如何有什么可以做的吗???我想要这样的结果:
array{[0]=> object(Character)=>{[id]=>'abc' [name]=> 'fyz'....} [1]=>object(Character){...}}
在学说示例上是否是两个不同的实体:学说本机查询示例
<?php
// Equivalent DQL query: "select u from User u join u.address a WHERE u.name = ?1"
// User owns association to an Address and the Address is loaded in the query.
$rsm = new ResultSetMapping;
$rsm->addEntityResult('User', 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addFieldResult('u', 'name', 'name');
$rsm->addJoinedEntityResult('Address' , 'a', 'u', 'address');
$rsm->addFieldResult('a', 'address_id', 'id');
$rsm->addFieldResult('a', 'street', 'street');
$rsm->addFieldResult('a', 'city', 'city');
$sql = 'SELECT u.id, u.name, a.id AS address_id, a.street, a.city FROM users u ' .
'INNER JOIN address a ON u.address_id = a.id WHERE u.name = ?';
$query = $this->_em->createNativeQuery($sql, $rsm);
$query->setParameter(1, 'romanb');
$users = $query->getResult();
如果您有这样的实体,则可以使用序列化器,我正在使用JMS序列化器:
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="login", type="string", length=255)
*/
private $login;
/**
* @var string
*
* @ORMColumn(name="char_name", type="string", length=255)
*/
private $charName;
/**
* @var string
*
* @ORMColumn(name="sex", type="string", length=255)
*/
private $sex;
/**
* @var string
*
* @ORMColumn(name="pvpkills", type="string", length=255)
*/
private $pvpkills;
/**
* @var string
*
* @ORMColumn(name="pkkills", type="string", length=255)
*/
private $pkkills;
/**
* @var bool
*
* @ORMColumn(name="online", type="boolean")
*/
private $online;
/**
* @var DateTime
*
* @ORMColumn(name="online_time", type="datetime")
*/
private $onlineTime;
/**
* @var DateTime
*
* @ORMColumn(name="last_access", type="datetime")
*/
private $lastAccess;
/**
* @var int
*
* @ORMColumn(name="lvl", type="integer")
*/
private $lvl;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* @var int
*
* @ORMColumn(name="clan_id", type="integer")
*/
private $clanId;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set login
*
* @param string $login
*
* @return Test
*/
public function setLogin($login)
{
$this->login = $login;
return $this;
}
/**
* Get login
*
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* Set charName
*
* @param string $charName
*
* @return Test
*/
public function setCharName($charName)
{
$this->charName = $charName;
return $this;
}
/**
* Get charName
*
* @return string
*/
public function getCharName()
{
return $this->charName;
}
/**
* Set sex
*
* @param string $sex
*
* @return Test
*/
public function setSex($sex)
{
$this->sex = $sex;
return $this;
}
/**
* Get sex
*
* @return string
*/
public function getSex()
{
return $this->sex;
}
/**
* Set pvpkilss
*
* @param string $pvpkills
*
* @return Test
*/
public function setPvpkills($pvpkills)
{
$this->pvpkills = $pvpkills;
return $this;
}
/**
* Get pvpkilss
*
* @return string
*/
public function getPvpkilss()
{
return $this->pvpkilss;
}
/**
* Set pkkills
*
* @param string $pkkills
*
* @return Test
*/
public function setPkkills($pkkills)
{
$this->pkkills = $pkkills;
return $this;
}
/**
* Get pkkills
*
* @return string
*/
public function getPkkills()
{
return $this->pkkills;
}
/**
* Set online
*
* @param boolean $online
*
* @return Test
*/
public function setOnline($online)
{
$this->online = $online;
return $this;
}
/**
* Get online
*
* @return bool
*/
public function getOnline()
{
return $this->online;
}
/**
* Set onlineTime
*
* @param DateTime $onlineTime
*
* @return Test
*/
public function setOnlineTime($onlineTime)
{
$this->onlineTime = $onlineTime;
return $this;
}
/**
* Get onlineTime
*
* @return DateTime
*/
public function getOnlineTime()
{
return $this->onlineTime;
}
/**
* Set lastAccess
*
* @param DateTime $lastAccess
*
* @return Test
*/
public function setLastAccess($lastAccess)
{
$this->lastAccess = $lastAccess;
return $this;
}
/**
* Get lastAccess
*
* @return DateTime
*/
public function getLastAccess()
{
return $this->lastAccess;
}
/**
* Set lvl
*
* @param integer $lvl
*
* @return Test
*/
public function setLvl($lvl)
{
$this->lvl = $lvl;
return $this;
}
/**
* Get lvl
*
* @return int
*/
public function getLvl()
{
return $this->lvl;
}
/**
* Set name
*
* @param string $name
*
* @return Test
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set clanId
*
* @param integer $clanId
*
* @return Test
*/
public function setClanId($clanId)
{
$this->clanId = $clanId;
return $this;
}
/**
* Get clanId
*
* @return int
*/
public function getClanId()
{
return $this->clanId;
}
因此,在执行查询后,您可以将数组序列化为对象。
$serializer = $this->get('jms_serializer');
$objects = $serializer->fromArray($data, 'array<NameSpaceYourBundleEntityTest>');
您将有一个想要的对象