簡體   English   中英

Sonata管理員捆綁包多對多關系未保存外部ID

[英]Sonata Admin Bundle Many-to-Many relationship not saving foreign ID

這是我的問題。 我有兩個具有多對多關系的實體,並想在一個字段中插入多個信息。 我使用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;
}
}

授課類別:

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

}

在此處輸入圖片說明 在我的表“ etudiant_notification”中沒有任何內容。

請對您的代碼進行以下更改(粗體):

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

並檢查會發生什么。 不保證,但是您可以嘗試一下。 機會良多!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM