2016-07-06 17 views
0

ここに私の問題があります。私は、多対多の関係を持つ2つのエンティティを持ち、フィールドに複数の情報を挿入したいと考えています。 私は投与のためのソナタ管理を使用して、ここに私のコードです:ソナタ管理バンドル外国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

あなたのコードを以下のように変更(太線)を作成してください:


/** 
* 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; 
} 

Notification.php

を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; 
} 

とチェック何が起こるのですか。保証はありませんが、試してみてください。ボンのチャンス!

+0

これは私にPHPエラー "CONTROL-Cでサーバーを終了しました。組み込みサーバーが予期せず終了しました。詳細については-vオプションを指定してコマンドを実行してください" –

+1

このコードにはstackoverflowがあります。たとえば、 'addNotification'を呼び出すと、このメソッドの内部で' addEtudiant'が呼び出され、 'addNotification'が呼び出されます。 'if($ notification-> getEtudiant()== $ this)'を使用して、スタックのオーバーフローを壊すようなチェックをします。 – svobol13

+0

@ svobol13ありがとう!はい、実際にコードに欠陥があります。変更は、ターゲット側ではなく、逆側にのみ追加する必要があります。 'Notification.php'の太字の行は追加する必要がありますが、' Etudiant.php'の太字の行は削除する必要があります。 – cezar