Hi, i use Donctrine 2.6.3 on a symfony 4.2 proyect(REST API).
and i have som trouble with some json generated
Theres is 2 basic entities :
class Usuarios{
/**
* @var int
*
* @ORM\Column(name="dni", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $dni;
/**
* @var string
*
* @ORM\Column(name="nyape", type="string", length=30, nullable=false)
*/
private $nyape;
public function getDni(): ?int{
return $this->dni;
}
public function getNyape(): ?string{
return $this->nyape;
}
public function setNyape(string $nyape): self{
$this->nyape = $nyape;
return $this;
}
/**
* @ORM\OneToMany(targetEntity="Infracciones", mappedBy="dni",
cascade={"all"})
* @var Doctrine\Common\Collection\ArrayCollection
*/
private $infracciones;
}
class Infracciones{
/**
* @var int
*
* @ORM\Column(name="nro_infraccion", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $nroInfraccion;
/**
* @var float
*
* @ORM\Column(name="monto", type="float", precision=10, scale=0,
nullable=false)
*/
private $monto;
/**
* @var \Usuarios
*
* @ORM\ManyToOne(targetEntity="Usuarios", inversedBy="infracciones")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="dni", referencedColumnName="dni")
* })
*/
private $dni;
public function getNroInfraccion(): ?int{
return $this->nroInfraccion;
}
public function getMonto(): ?float{
return $this->monto;
}
public function setMonto(float $monto): self{
$this->monto = $monto;
return $this;
}
public function getDni(): ?Usuarios{
return $this->dni;
}
public function setDni(?Usuarios $dni): self{
$this->dni = $dni;
return $this;
}
}
In some controller there is a method:
public function searchInfraccionesDeUsuarioPorDniV2($dni){
$entityManager = $this->getDoctrine()->getEntityManager();
$query = $entityManager->createQuery('SELECT u,i
FROM App\Entity\Infracciones u
JOIN u.dni i where i.dni=11111111');
$usuarios = $query->getArrayResult();
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($usuarios));
return $response;
}
This generate ths json :
[
{
"nroInfraccion":1,
"monto":11,
"dni":{
"dni":11111111,
"nyape":"Lucas"
}
},
{
"nroInfraccion":2,
"monto":22,
"dni":{
"dni":11111111,
"nyape":"Lucas"
}
}
]
It's not what i need.
I need this json format:
{ "dni":11111111, "nyape":"Lucas", "infracciones":[ { "nroInfraccion":1,
"monto":11 }, { "nroInfraccion":2, "monto":22 } ] }
I think that there's a problem with the owner of the
relation(mappedby?inverdedby?).
Is it a problem with the join?
How can a reverse the owner of a relation?
Thank you in advance.
Julian
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.