Sonata Admin Bundle多对多关系不保存外国ID

时间:2016-07-06 03:18:23

标签: php symfony sonata-admin symfony-2.7

这是我的问题。我有两个具有多对多关系的实体,并希望在字段中插入多个信息。 我使用sonata admin进行管理,这是我的代码:

通知类:

<?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 Class:

    <?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 个答案:

答案 0 :(得分:2)

请对您的代码进行以下更改(粗线):

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 chance!