Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Sonata管理包多对多关系不保存外部ID_Php_Symfony_Sonata Admin_Symfony 2.7 - Fatal编程技术网

Php Sonata管理包多对多关系不保存外部ID

Php Sonata管理包多对多关系不保存外部ID,php,symfony,sonata-admin,symfony-2.7,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 { /**

这是我的问题。我有两个具有多对多关系的实体,希望在字段中插入多个信息。 我使用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;
}
}

请对代码进行以下更改(粗体行):

Notification.php


/**
*添加etudiant
*
*@param\App\BlogBundle\Entity\Etudiant$Etudiant
*@退货通知
*/
公共函数addudiant(\App\BlogBundle\Entity\Etudiant$Etudiant)
{
$etudiant->addNotification($this);
$this->etudiant[]=$etudiant;
退还$this;
}
Etudiant.php


/**
*添加通知
*
*@param\App\BlogBundle\Entity\Notification$Notification
*@return-Etudiant
*/
公共函数addNotification(\App\BlogBundle\Entity\Notification$Notification)
{
$notification->additudiant($this);
$this->notification[]=$notification;
退还$this;
}

看看会发生什么。不保证,但你可以试一试。好机会

这给了我一个PHP错误“退出带有CONTROL-C的服务器。内置服务器意外终止使用-v选项再次运行命令以获取更多详细信息”。此代码中存在stackoverflow。例如,如果您调用
addNotification
,则此方法的内部被调用
addudiant
,从而导致调用
addNotification
。使用
if($notification->getEtudiant()==$this)
类似的检查来打破堆栈溢出。@svobol13谢谢,伙计!是的,代码中确实存在缺陷。更改只应添加到反向侧,而不是目标侧。应添加
Notification.php
中的粗体行,但应删除
Etudiant.php
中的粗体行。
    <?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;
    }
}
<?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;
    }

}

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

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