Julien Julien - 2 months ago 6
PHP Question

'NotNullConstraintViolationException' Error when flush() on Symfony 3

I'm starting to learn Symfony 3, but i encountered an error when trying to flush an Objet on my Database using Doctrine.

On my main Controller, I instantiate an Objet on which I set some properties default values.

<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Objet;
use AppBundle\Entity\Membre;

class DefaultController extends Controller
{
/**
* @Route("/new", name="nouvelobjet")
*/
public function newAction(Request $request)
{
$objet = new Objet();
$objet->setIdVendeur('2');
$objet->setTitre('Article de test');
$em = $this->getDoctrine()->getManager();
$em->persist($objet);
$em->flush();
return new Response ('<body>Id_vendeur:'.$objet->getIdVendeur().'</body>');
}
}
?>


Here is the Classe 'Objet' I use :

<?php
// src/AppBundle/Entity/Objet.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="objet")
*/

class Objet
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id_article;

/**
* @ORM\Column(type="integer")
*/
private $id_vendeur;

/**
* @ORM\Column(type="string")
*/
private $titre;

/**
* @ORM\ManyToOne(targetEntity="Membre", inversedBy="objets")
* @ORM\JoinColumn(name="id_vendeur", referencedColumnName="id")
*/
private $membre;

/**
* Get idArticle
*
* @return integer
*/
public function getIdArticle()
{
return $this->id_article;
}

/**
* Set idVendeur
*
* @param integer $idVendeur
*
* @return Objet
*/
public function setIdVendeur($idVendeur)
{
$this->id_vendeur = $idVendeur;

return $this;
}

/**
* Get idVendeur
*
* @return integer
*/
public function getIdVendeur()
{
return $this->id_vendeur;
}

/**
* Set titre
*
* @param string $titre
*
* @return Objet
*/
public function setTitre($titre)
{
$this->titre = $titre;

return $this;
}

/**
* Get titre
*
* @return string
*/
public function getTitre()
{
return $this->titre;
}


/**
* Set membre
*
* @param \AppBundle\Entity\Membre $membre
*
* @return Objet
*/
public function setMembre(\AppBundle\Entity\Membre $membre = null)
{
$this->membre = $membre;

return $this;
}

/**
* Get membre
*
* @return \AppBundle\Entity\Membre
*/
public function getMembre()
{
return $this->membre;
}
}


and this is the class 'Membre' (not sure it's really useful there):

<?php
// src/AppBundle/Entity/Membre.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
/**
* Membre
*
* @ORM\Table(name="membre")
* @ORM\Entity(repositoryClass="AppBundle\Repository\MembreRepository")
*/
class Membre
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;

/**
* @ORM\OneToMany(targetEntity="Objet", mappedBy="membre")
*/
private $objets;

public function __construct()
{
$this->objets = new ArrayCollection();
}

/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Set name
*
* @param string $name
*
* @return Membre
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Add objet
*
* @param \AppBundle\Entity\Objet $objet
*
* @return Membre
*/
public function addObjet(\AppBundle\Entity\Objet $objet)
{
$this->objets[] = $objet;

return $this;
}

/**
* Remove objet
*
* @param \AppBundle\Entity\Objet $objet
*/
public function removeObjet(\AppBundle\Entity\Objet $objet)
{
$this->objets->removeElement($objet);
}

/**
* Get objets
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getObjets()
{
return $this->objets;
}
}


When I go the url of my controller, i throw an error :


An exception occurred while executing 'INSERT INTO objet (id_vendeur, titre) VALUES (?, ?)' with params [null, "Article de test"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id_vendeur' cannot be null


When I comment the '$em->flush();' line, I can print the value of getIdVendeur() without problem, so it's not a null value.

It makes 3 days i'm stuck on this problem... can someone help me about this ? thanks in advance !

Answer

Assuming that in the database object Membre with id = 2 exist you can do the following in your controller:

public function newAction(Request $request)
    {
        $objet = new Objet();
        $objet->setIdVendeur('2');
        $objet->setTitre('Article de test');
        $em = $this->getDoctrine()->getManager();

        // Retreive the object instance with id = 2
        $membre = $em->find(Membre::class, 2);
        // Set/specify the doctrine relations  
        $objet->setMembre($membre);   

        $em->persist($objet);
        $em->flush();
        return new Response ('<body>Id_vendeur:'.$objet->getIdVendeur().'</body>');
    }

Probably you don't need the attribute id_vendeur of the class Objet.

Hope this help

Comments