我有两个具有ManyToMany关系的实体(学生,通知),我想向多个学生插入通知,我使用SonataAdmin管理应用程序这是我的代码: 实体学生:
<?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\OneToMany(targetEntity="Notification",mappedBy="Etudiant")
*/
private $notification;
public function __construct() {
$this->notification = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* 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;
}
/**
* 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;
}
public function __toString() {
return $this->nom;
}
}
实体通知:
<?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\ManyToOne(targetEntity="Etudiant",inversedBy="notification")
*@ORM\JoinColumn(name="etudiant_id",referencedColumnName="id")
*/
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;
}
/**
* Set etudiant
*
* @param \App\BlogBundle\Entity\Etudiant $etudiant
* @return Notification
*/
public function setEtudiant(\App\BlogBundle\Entity\Etudiant $etudiant = null)
{
$this->etudiant = $etudiant;
return $this;
}
/**
* Get etudiant
*
* @return \App\BlogBundle\Entity\Etudiant
*/
public function getEtudiant()
{
return $this->etudiant;
}
public function __toString() {
return $this->message;
}
}
错误是:
类型错误:传递给App \ BlogBundle \ Entity \ Notification :: setEtudiant()的参数1必须是App \ BlogBundle \ Entity \ Etudiant的实例,Doctrine \ Common \ Collections \ ArrayCollection的实例,在/ var中调用第556行/www/html/School/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php
答案 0 :(得分:0)
如错误所述,您的setEtudiant
定义错误。它应该以Doctrine Collection
作为参数而不是Etudiant
实例。