I have two Plates and Candidate tables with the same structure:
TABLE Chapa (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
)
TABLE Candidato (
id INT AUTO_INCREMENT NOT NULL,
chapa_id INT DEFAULT NULL,
PRIMARY KEY(id)
)
And the respective entities:
<?php
use Doctrine\Common\Collections\ArrayCollection;
/** @Entity */
class Chapa
{
// ...
/**
* Uma Chapa possui Muitos Candidatos.
* @OneToMany(targetEntity="Candidato", mappedBy="chapa")
*/
private $candidatos;
// ...
public function __construct() {
$this->candidatos = new ArrayCollection();
}
}
/** @Entity */
class Candidato
{
// ...
/**
* Muitos Candidaatos possuem Uma Chapa.
* @ManyToOne(targetEntity="Chapa", inversedBy="candidatos")
* @JoinColumn(name="chapa_id", referencedColumnName="id")
*/
private $chapa;
// ...
}
With the following queryBuilder I get the result:
$qb = $this->createQueryBuilder('ch')
->select('ch.numero, cds.nome' )
->leftJoin('ch.candidatos', 'cds')
->getQuery()
->getResult();
// Resultado
{
"data": [
{
"numero": "Chapa 10",
"nome": "Candidato 1"
},
{
"numero": "Chapa 10",
"nome": "Candidato 2"
}
],
"success": true
}
How do I get the following result (nested)?
{
"data": [
{
"numero": "Chapa 10",
"candidatos": [
{
"nome": "Candidato 1"
},
{
"nome": "Candidato 2"
}
]
}
],
"success": true
}