I've FabricanteDistribuidor.php
entity with this code:
class FabricanteDistribuidor
{
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Pais", mappedBy="fabricanteDistribuidor", cascade={"persist"})
*/
private $paises;
/**
* Set paises
*
* @param \AppBundle\Entity\Pais $pais
* @return FabricanteDistribuidor
*/
public function setPaises(\Sencamer\AppBundle\Entity\Pais $pais)
{
$this->paises[] = $pais;
return $this;
}
/**
* Get paises
*
* @return Doctrine\Common\Collections\Collection
*/
public function getPaises()
{
return $this->paises;
}
}
And the related Pais.php
entity code:
class Pais
{
use IdentifierAutogeneratedEntityTrait;
use NamedEntityTrait;
use ActiveEntityTrait;
/**
* @ORM\ManyToMany(targetEntity="Sencamer\AppBundle\Entity\FabricanteDistribuidor", inversedBy="paises", cascade={"persist"})
* @ORM\JoinTable(name="negocio.fabricante_distribuidor_pais", schema="negocio",
* joinColumns={@ORM\JoinColumn(name="fabricante_distribuidor", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="pais_id", referencedColumnName="id")}
* )
*/
protected $fabricanteDistribuidor;
/**
* Add fabricanteDistribuidor
*
* @param AppBundle\Entity\FabricanteDistribuidor $fabricanteDistribuidor
*/
public function addfabricanteDistribuidor(\AppBundle\Entity\FabricanteDistribuidor $fabricanteDistribuidor)
{
$this->fabricanteDistribuidor[] = $fabricanteDistribuidor;
}
/**
* Get fabricanteDistribuidor
*
* @return Doctrine\Common\Collections\Collection
*/
public function getfabricanteDistribuidor()
{
return $this->fabricanteDistribuidor;
}
}
I'm trying to get now the related paises
from FabricanteDistribuidor
as follow and I'm doing something wrong since I can't get their names, so what is wrong in my code?
public function obtenerDetallesFabricanteAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:FabricanteDistribuidor')->find($request->query->get('id'));
if ($request->isXmlHttpRequest()) {
$response['entities'] = array();
$dataResponse = array();
// rest of columns ....
if ($entity->getPaises() instanceof Pais) {
$paises = array();
foreach ($entity->getPaises() as $pais) {
$paises[] = $pais->getNombre();
}
$dataResponse['paises'] = $paises;
}
$response['entities'][] = $dataResponse;
return new JsonResponse($response);
}
}
Maybe the right way is go through a DQL in a Doctrine Repository but then is the performance problem and here I'm not an expert so need a clue or a workaround on this.
Now with this same classes I've also a concern related the Pais
class because as you notice I added the inversed side $fabricanteDistribuidor
so, do I have to insert this any time I want to insert a new Pais
or is just to tell Doctrine how to deal with proxies inside it? I've not clear yet how owning/inversed side works yet maybe due to this I did thing as my code shown. Any advice around this too?
May be you're missing to initialize $paises
(and also $fabricanteDistribuidor
) collection? Shouldn't it be look like this?:
class FabricanteDistribuidor
{
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Pais", mappedBy="fabricanteDistribuidor", cascade={"persist"})
*/
private $paises;
public function __construct()
{
$this->paises = new ArrayCollection(); // don't forget to import it
}
....
}