2017-09-11 1 views
1

Nous avons une erreur en essayant de créer une relation dans les 2 tableaux comme celui-cierreur Symfony3 avec ManyToMany avec des attributs

Llamadas - || - | < - LlamadaDerivada -> | - || - PersonaDerivada

Et nous essayons de créer un seul formulaire de création avec le "LlamadaDerivada" dedans.

intérieur entité Llamada

<?php 

namespace xxxxBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

class Llamada { 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="SEQUENCE") 
    * @ORM\SequenceGenerator(sequenceName="llamada_id_seq", allocationSize=1, initialValue=1) 
    */ 
    private $id; 

    /** 
    * @var string 
    * @Assert\Length(
    *  max = 50, 
    *  maxMessage = "Your first name cannot be longer than {{ limit }} characters", 
    *) 
    * @ORM\Column(name="nombre", type="string", length=150, nullable=false) 
    */ 
    private $nombre; 
    /** 
    * 
    * @ORM\OneToMany(targetEntity="LlamadaDerivado", mappedBy="llamada") 
    */ 
    private $derivados; 

    function __construct() { 
     $this->derivados = new ArrayCollection(); 
    } 

    function getId() { 
     return $this->id; 
    } 

    function getNombre() { 
     return $this->nombre; 
    } 

    function setId($id) { 
     $this->id = $id; 
    } 

    function setNombre($nombre) { 
     $this->nombre = $nombre; 
    } 

    function getDerivados(){ 
     return $this->derivados; 
    } 
    function setDerivados($derivados){ 
     $this->derivados = $derivados; 
    } 
} 

alors à l'intérieur LlamadaDerivado Entité nous avons ce

<?php 

namespace xxxBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* LlamadaDerivado 
* 
* @ORM\Table(name="llamada_derivado") 
* @ORM\Entity 
*/ 
class LlamadaDerivado 
{ 

    /** 
    * @var \AgendaBundle\Entity\Llamada 
    * 
    * @ORM\ManyToOne(targetEntity="AgendaBundle\Entity\Llamada",inversedBy="derivados",cascade={"persist"}) 
    * @ORM\Id 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="id_llamada", referencedColumnName="id") 
    * }) 
    */ 
    private $llamada; 

    /** 
    * @var \AgendaBundle\Entity\PersonaDerivado 
    * 
    * @ORM\ManyToOne(targetEntity="AgendaBundle\Entity\PersonaDerivado",inversedBy="llamadas",cascade={"persist"}) 
    * @ORM\Id 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="id_derivado", referencedColumnName="id") 
    * }) 
    */ 
    private $derivado; 


    /** 
    * @var DateTime 
    * 
    * @ORM\Column(name="fecha_derivacion", type="date", nullable=false) 
    */ 
    private $fechaDerivacion; 


    function getLlamada(){ 
     return $this->llamada; 
    } 

    function getDerivado(){ 
     return $this->derivado; 
    } 

    function getFechaDerivacion() { 
     return $this->fechaDerivacion; 
    } 

    function setLlamada($llamada) { 
     $this->llamada = $llamada; 
    } 

    function setDerivado($derivado) { 
     $this->derivado = $derivado; 
    } 

    function setFechaDerivacion($fechaDerivacion) { 
     $this->fechaDerivacion = $fechaDerivacion; 
    } 

} 

Et à l'intérieur entité PersonaDerivado

<?php 

namespace xxxBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* ReunionLugar 
* 
* @ORM\Table(name="persona_derivado") 
* @ORM\Entity 
*/ 
class PersonaDerivado 
{ 

    public function __construct() { 
     $this->llamadas = new ArrayCollection(); 
    } 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="SEQUENCE") 
    * @ORM\SequenceGenerator(sequenceName="reunion_lugar_id_seq", allocationSize=1, initialValue=1) 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="nombre", type="string", length=150, nullable=false) 
    */ 
    private $nombre; 

    /** 
    * @ORM\OneToMany(targetEntity="LlamadaDerivado", mappedBy="derivado") 
    */ 
    private $llamadas; 

    function getId() { 
     return $this->id; 
    } 

    function getNombre() { 
     return $this->nombre; 
    } 

    function setId($id) { 
     $this->id = $id; 
    } 

    function setNombre($nombre) { 
     $this->nombre = $nombre; 
    } 

    function setLlamadas($llamadas) { 
     $this->llamadas = $llamadas; 
    } 

} 

Et le LlamadaType est

class LlamadaDto extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $disabled = $options['disabled']; 


     $builder 
       ->add('id', HiddenType::class) 
       ->add('nombre', TextType::class, array(
        'disabled' => $disabled, 
        'attr' => ['maxlength'=>'50'] 
       ))->add('apellido', TextType::class, array(
        'disabled' => $disabled, 
        'attr' => ['maxlength'=>'50'] 
       ))->add('fecha', DateType::class, array(
        'format' => 'dd/MM/yyyy', 
        'disabled' => $disabled, 
        'widget' => 'single_text', 
        'attr' => ['class' => 'datepicker'] 
       ))->add('hora', TimeType::class, array(
        'disabled' => $disabled 
       ))->add('motivo', TextareaType::class, array(
        'disabled' => $disabled, 
        'attr' => ['maxlength'=>'400'] 
       ))->add('telefonoContacto', TextType::class, array(
        'disabled' => $disabled, 
        'attr' => ['maxlength'=>'9'] 
       ))->add('derivados', EntityType::class, array(
        'class' => 'AgendaBundle:PersonaDerivado', 
        'choice_label' => 'apellidoNombre', 
        'placeholder' => 'Seleccionar un derivado', 
        'multiple' => true, 
       )); 
    } 

    public function configureOptions(OptionsResolver$resolver) { 
     $resolver->setDefaults(array('data_class' => Llamada::class)); 
    } 

} 

A l'intérieur du contrôleur, nous avons ce code

<?php 

/** 
    * @Route("/Llamada/save",name="saveLlamada") 
    */ 
    public function saveLlamadaAction(Request $request) { 
     $llamadaService = $this->get('llamadaService'); 
     $derivadoService = $this->get('derivadoService'); 

     $form = $this->createForm(LlamadaDto::class); 
     $form->handleRequest($request); 

     $editar = TRUE; 
     $llamada = $form->getData(); 

     $derivados = $request->request->get("llamada_dto")["derivados"]; 


     $derivadosActuales = $derivadoService->getLlamadaDerivados($llamada->getId()); 

     foreach ($derivados as $key1 => $d) { 
      foreach ($derivadosActuales as $key2 => $da) { 
       if($da->getDerivado()->getId()==$d){ 
        array_splice($derivados, array_search($d, $derivados),1); 
       } 
      } 
     } 

     if ($llamadaService->saveLlamada($llamada)) { 
      $this->addFlash(
        'exitoLlamada', 'Datos de llamada guardados exitosamente' 
      ); 

      $derivadoService->saveDerivados($derivados,$llamada); 
     } else { 
      $this->addFlash(
        'errorLlamada', 'Disculpe, hubo un error en el registro de la llamada' 
      ); 
     } 
     return new RedirectResponse($this->generateUrl('listaLlamadas', array(), UrlGeneratorInterface::RELATIVE_PATH)); 
    } 

Et les services appelés sont ce les:

public function saveLlamada($llamada){ 
    try{ 
    if($llamada->getId()){ 
     $this->em->merge($llamada); 
    }else{ 
     $this->em->persist($llamada); 
    } 

    $this->em->flush(); 
    return TRUE; 
    } catch (Exception $ex){ 
     return FALSE; 
    } 
} 

public function saveDerivados($derivados,$llamada){ 
    foreach ($derivados as $key => $derivado) { 
     $llamadaDerivado = new LlamadaDerivado(); 
     $personaLlamada = $this->getDerivado($derivado); 

     $llamadaDerivado->setLlamada($llamada); 
     $llamadaDerivado->setDerivado($personaLlamada); 
     $llamadaDerivado->setFechaDerivacion(new \DateTime('now', (new \DateTimeZone('America/Argentina/Ushuaia')))); 
     $this->em->persist($llamadaDerivado); 
     $this->em->flush(); 
    } 
} 

C'est l'erreur que nous obtenons:

Uncaught PHP Exception Doctrine \ ORM \ ORMInvalidArgumentException: "Valeur attendue de type" Doctrine \ Common \ Collections \ Collection "pour le champ d'association" xxxBun dle \ Entity \ Llamada # $ derivados ", a obtenu" xxxBundle \ Entity \ PersonaDerivado "à la place." au projet \ fournisseur \ doctrine \ ORM \ lib \ Doctrine \ ORM \ ORMInvalidArgumentException.php ligne 206

Nous avons été 1 semaine avec cela. Un grand merci à l'avance

Répondre

0

oeil à l'entité Llamada, derivados de $ est une collection de tableau de LlamadaDerivado et dans votre LlamadaType vous faire entityType de AgendaBundle: PersonaDerivado, c'est la raison pour laquelle vous avez obtenu cette erreur. Pensez à utiliser Type de collection pour ajouter à chaque fois tout l'objet LlamadaDerivado pour bien respecter le mapping.

+0

Merci pour votre réponse. J'ai essayé de faire cela, mais le problème est que quand je fais cela, chargez seulement les entités que j'ai dans la relation, et j'ai besoin que la liste d'options montre tous les avalaibles "personaDerivada". –

0

Vous pouvez essayer d'utiliser un CollectionType au lieu de EntityType dans votre formulaire, bien que j'ai un morceau de code devant moi qui fonctionne parfaitement avec EntityType et le drapeau multiple pour une relation OneToMany.

+0

Merci pour votre réponse. J'ai essayé de faire cela, mais le problème est que quand je fais cela, chargez seulement les entités que j'ai dans la relation, et j'ai besoin que la liste d'options montre tous les avalaibles "personaDerivada". –