-1

I have two entities with ManyToMany relationship (Student, Notification) and I want to insert a notification with multiple students, I use SonataAdmin for the administration of the application Here is my code: Entitiy Student:

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

Entity Notification:

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

The error is :

Type error: Argument 1 passed to App\BlogBundle\Entity\Notification::setEtudiant() must be an instance of App\BlogBundle\Entity\Etudiant, instance of Doctrine\Common\Collections\ArrayCollection given, called in /var/www/html/School/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 556

Nacer Naciri
  • 67
  • 2
  • 8

1 Answers1

0

As the error states, your setEtudiant definition is wrong. It should take a Doctrine Collection as parameter instead of an Etudiant instance.

Terenoth
  • 2,458
  • 1
  • 14
  • 21