2016-07-06 300 views
0

这是我的问题。我有两个具有多对多关系的实体,并希望在字段中插入多个信息。 我使用管理奏鸣曲管理,这里是我的代码:Sonata Admin Bundle多对多的关系不保存国外ID

通知类:

<?php 

namespace App\BlogBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Notification 
* 
* @ORM\Table(name="notification") 
* @ORM\Entity 
*/ 
class Notification { 

/** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
private $id; 

/** 
    * @var string 
    * 
    * @ORM\Column(name="message", type="string", length=255) 
    */ 
private $message; 

/** 
    * @ORM\ManyToMany(targetEntity="Etudiant",mappedBy="notification") 
    */ 
private $etudiant; 

/** 
    * Get id 
    * 
    * @return integer 
    */ 
public function getId() { 
    return $this->id; 
} 

/** 
* Set message 
* 
* @param string $message 
* @return Notification 
*/ 
public function setMessage($message) { 
    $this->message = $message; 

    return $this; 
} 

/** 
* Get message 
* 
* @return string 
*/ 
public function getMessage() { 
    return $this->message; 
} 

public function __toString() { 
    return $this->message; 
} 

public function __construct() { 
    $this->etudiant = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

public function setEtudiant($etudiant) { 
    if (count($etudiant) > 0) { 
     foreach ($etudiant as $i) { 
      $this->addEtudiant($i); 
     } 
    } 
    return $this; 
} 

/** 
* Add etudiant 
* 
* @param \App\BlogBundle\Entity\Etudiant $etudiant 
* @return Notification 
*/ 
public function addEtudiant(\App\BlogBundle\Entity\Etudiant $etudiant) 
{ 
    $this->etudiant[] = $etudiant; 

    return $this; 
} 

/** 
* Remove etudiant 
* 
* @param \App\BlogBundle\Entity\Etudiant $etudiant 
*/ 
public function removeEtudiant(\App\BlogBundle\Entity\Etudiant $etudiant) 
{ 
    $this->etudiant->removeElement($etudiant); 
} 

/** 
* Get etudiant 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getEtudiant() 
{ 
    return $this->etudiant; 
} 
} 

Etudiant类:

<?php 

namespace App\BlogBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Etudiant 
* 
* @ORM\Table(name="etudiant") 
* @ORM\Entity 
*/ 
class Etudiant{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="nom", type="string", length=255) 
    */ 
    private $nom; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="prenom", type="string", length=255) 
    */ 
    private $prenom; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date_naissance", type="datetime") 
    */ 
    private $dateNaissance; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="adresse", type="text") 
    */ 
    private $adresse; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="email", type="string", length=255) 
    */ 
    private $email; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="telephone", type="integer") 
    */ 
    private $telephone; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="num_inscription", type="string", length=255) 
    */ 
    private $numInscription; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date_inscription", type="datetime") 
    */ 
    private $dateInscription; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="frais_scolarite", type="integer") 
    */ 
    private $fraisScolarite; 

    /** 
    * @ORM\ManyToMany(targetEntity="Notification",inversedBy="etudiant") 
    *@ORM\JoinColumn(name="user_notificaiton") 
    */ 
    private $notification; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set nom 
    * 
    * @param string $nom 
    * @return Etudiant 
    */ 
    public function setNom($nom) 
    { 
     $this->nom = $nom; 

     return $this; 
    } 

    /** 
    * Get nom 
    * 
    * @return string 
    */ 
    public function getNom() 
    { 
     return $this->nom; 
    } 

    /** 
    * Set prenom 
    * 
    * @param string $prenom 
    * @return Etudiant 
    */ 
    public function setPrenom($prenom) 
    { 
     $this->prenom = $prenom; 

     return $this; 
    } 

    /** 
    * Get prenom 
    * 
    * @return string 
    */ 
    public function getPrenom() 
    { 
     return $this->prenom; 
    } 

    /** 
    * Set dateNaissance 
    * 
    * @param \DateTime $dateNaissance 
    * @return Etudiant 
    */ 
    public function setDateNaissance($dateNaissance) 
    { 
     $this->dateNaissance = $dateNaissance; 

     return $this; 
    } 

    /** 
    * Get dateNaissance 
    * 
    * @return \DateTime 
    */ 
    public function getDateNaissance() 
    { 
     return $this->dateNaissance; 
    } 

    /** 
    * Set adresse 
    * 
    * @param string $adresse 
    * @return Etudiant 
    */ 
    public function setAdresse($adresse) 
    { 
     $this->adresse = $adresse; 

     return $this; 
    } 

    /** 
    * Get adresse 
    * 
    * @return string 
    */ 
    public function getAdresse() 
    { 
     return $this->adresse; 
    } 

    /** 
    * Set email 
    * 
    * @param string $email 
    * @return Etudiant 
    */ 
    public function setEmail($email) 
    { 
     $this->email = $email; 

     return $this; 
    } 

    /** 
    * Get email 
    * 
    * @return string 
    */ 
    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * Set telephone 
    * 
    * @param integer $telephone 
    * @return Etudiant 
    */ 
    public function setTelephone($telephone) 
    { 
     $this->telephone = $telephone; 

     return $this; 
    } 

    /** 
    * Get telephone 
    * 
    * @return integer 
    */ 
    public function getTelephone() 
    { 
     return $this->telephone; 
    } 

    /** 
    * Set numInscription 
    * 
    * @param string $numInscription 
    * @return Etudiant 
    */ 
    public function setNumInscription($numInscription) 
    { 
     $this->numInscription = $numInscription; 

     return $this; 
    } 

    /** 
    * Get numInscription 
    * 
    * @return string 
    */ 
    public function getNumInscription() 
    { 
     return $this->numInscription; 
    } 

    /** 
    * Set dateInscription 
    * 
    * @param \DateTime $dateInscription 
    * @return Etudiant 
    */ 
    public function setDateInscription($dateInscription) 
    { 
     $this->dateInscription = $dateInscription; 

     return $this; 
    } 

    /** 
    * Get dateInscription 
    * 
    * @return \DateTime 
    */ 
    public function getDateInscription() 
    { 
     return $this->dateInscription; 
    } 

    /** 
    * Set fraisScolarite 
    * 
    * @param integer $fraisScolarite 
    * @return Etudiant 
    */ 
    public function setFraisScolarite($fraisScolarite) 
    { 
     $this->fraisScolarite = $fraisScolarite; 

     return $this; 
    } 

    /** 
    * Get fraisScolarite 
    * 
    * @return integer 
    */ 
    public function getFraisScolarite() 
    { 
     return $this->fraisScolarite; 
    } 

    public function __toString() { 
     return $this->nom; 
    } 

    public function __construct() { 
     $this->notification = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 


    function setNotification($notification) { 
     if (count($notification) > 0) { 
      foreach ($notification as $i) { 
       $this->addEtudiant($i); 
      } 
     } 
     return $this; 
    } 

     /** 
    * Add notification 
    * 
    * @param \App\BlogBundle\Entity\Notification $notification 
    * @return Etudiant 
    */ 
    public function addNotification(\App\BlogBundle\Entity\Notification $notification) 
    { 
     $this->notification[] = $notification; 

     return $this; 
    } 

    /** 
    * Remove notification 
    * 
    * @param \App\BlogBundle\Entity\Notification $notification 
    */ 
    public function removeNotification(\App\BlogBundle\Entity\Notification $notification) 
    { 
     $this->notification->removeElement($notification); 
    } 

    /** 
    * Get notification 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getNotification() 
    { 
     return $this->notification; 
    } 
} 

最后我NotificationAdmin:

<?php 

namespace App\BlogBundle\Admin; 

use Sonata\AdminBundle\Admin\Admin; 
use Sonata\AdminBundle\Show\ShowMapper; 
use Sonata\AdminBundle\Form\FormMapper; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 

class NotificationAdmin extends Admin { 

    // Fields to be shown on create/edit forms 
    protected function configureFormFields(FormMapper $formMapper) { 
     $formMapper 
       ->add('message', 'text') 
       ->add('etudiant', 'sonata_type_model', array(
        'required' => false, 
        'multiple' => true 
       )) 
     ; 
    } 

    // Fields to be shown on filter forms 
    protected function configureDatagridFilters(DatagridMapper $datagridMapper) { 
     $datagridMapper 
       ->add('message') 
       ->add('etudiant') 
     ; 
    } 

    // Fields to be shown on lists 
    protected function configureListFields(ListMapper $listMapper) { 
     $listMapper 
       ->addIdentifier('message') 
       ->add('etudiant') 
     ; 
    } 

    // Fields to be shown on show action 
    protected function configureShowFields(ShowMapper $showMapper) { 
     $showMapper 
       ->add('id') 
       ->add('nom') 

     ; 
    } 

    public function prePersist($notification){ 
     $this->preUpdate($notification); 
    } 
    public function preUpdate($notification){ 
     $notification->setEtudiant($notification); 
    } 

    public function getBatchActions() { 
     // retrieve the default batch actions (currently only delete) 
     $actions = parent::getBatchActions(); 

     if (
       $this->hasRoute('edit') && $this->isGranted('EDIT') && 
       $this->hasRoute('delete') && $this->isGranted('DELETE') 
     ) { 
      $actions['merge'] = array(
       'label' => 'action_merge', 
       'translation_domain' => 'SonataAdminBundle', 
       'ask_confirmation' => true 
      ); 
     } 

     return $actions; 
    } 

} 

enter image description here 没有什么在我的表“etudiant_notification”。 三江源,

回答

1

请做如下修改(粗线)到您的代码:

Notification.php


/** 
* Add etudiant 
* 
* @param \App\BlogBundle\Entity\Etudiant $etudiant 
* @return Notification 
*/ 
public function addEtudiant(\App\BlogBundle\Entity\Etudiant $etudiant) 
{ 
    $etudiant->addNotification($this); 
    $this->etudiant[] = $etudiant; 

    return $this; 
} 

Etudiant.php


/** 
* Add notification 
* 
* @param \App\BlogBundle\Entity\Notification $notification 
* @return Etudiant 
*/ 
public function addNotification(\App\BlogBundle\Entity\Notification $notification) 
{ 
    $notification->addEtudiant($this); 
    $this->notification[] = $notification; 

    return $this; 
} 

和检查怎么了。没有保证,但你可以试试看。 Bonne的机会!

+0

这给了我一个PHP错误“使用CONTROL-C退出服务器。内置服务器意外终止使用-v选项再次运行此命令以获取更多详细信息” –

+1

此代码中有stackoverflow。例如,如果您调用'addNotification',则在此方法内调用'addEtudiant',这会导致调用'addNotification'。使用'if($ notification-> getEtudiant()== $ this)'像检查来打破堆栈溢出。 – svobol13

+0

@ svobol13谢谢朋友!是的,代码中确实存在缺陷。改变只应该添加到反面,而不是目标面。应该添加'Notification.php'中的粗体行,但应删除'Etudiant.php'中的粗体行。 – cezar