我有这样的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){...}}
答案 0 :(得分:0)
关于doctrine示例,如果是两个不同的实体:Doctrine Native Query Example
<?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 Serializer的序列化器:
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="login", type="string", length=255)
*/
private $login;
/**
* @var string
*
* @ORM\Column(name="char_name", type="string", length=255)
*/
private $charName;
/**
* @var string
*
* @ORM\Column(name="sex", type="string", length=255)
*/
private $sex;
/**
* @var string
*
* @ORM\Column(name="pvpkills", type="string", length=255)
*/
private $pvpkills;
/**
* @var string
*
* @ORM\Column(name="pkkills", type="string", length=255)
*/
private $pkkills;
/**
* @var bool
*
* @ORM\Column(name="online", type="boolean")
*/
private $online;
/**
* @var \DateTime
*
* @ORM\Column(name="online_time", type="datetime")
*/
private $onlineTime;
/**
* @var \DateTime
*
* @ORM\Column(name="last_access", type="datetime")
*/
private $lastAccess;
/**
* @var int
*
* @ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var int
*
* @ORM\Column(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<NameSpace\YourBundle\Entity\Test>');
您将拥有一个您想要的对象