简体   繁体   中英

Symfony : Doctrine doesn't update ManyToOne

When I try to add a object From my Entity 'Facture' Doctrine does not update object 'Dechet'. The insert works correctly but in my table Dechet I don't have my foreign key from my Object (Facture) :

                    if($dechetVert>$CorrecteurDechetVert || $Encombrants>$CorrecteurEncombrants || $Bois>$CorrecteurBois ||
                    $Brique>$CorrecteurBrique || $Terre>$CorrecteurTerre) {
                    $time = new \DateTime('now');
                    $Facture = new Facture();
                    $Facture->setDate($time);
                    $Facture->setStatut('Impayé');
                    $repository = $em->getRepository('Bundles\UserBundle\Entity\User');
                    $menage = $repository->findOneById($UserId);
                    $repository = $em->getRepository('Bundles\MainBundle\Entity\TypeDechet');
                    $type =$repository->findBy(array('Nom'=>$Type));
                    $repository = $em->getRepository('Bundles\MainBundle\Entity\Dechet');
                    $DechetsSelectionne = $repository->getDechetByMenageAndType($menage,$type);
                    $Facture->setUtilisateur($menage);
                    $Facture->setDechets($DechetsSelectionne);

                    $em->persist($Facture);
                    $em->flush();
                    $em->clear();

Entity Dechet :

    <?php

namespace Bundles\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sensio\Bundle\GeneratorBundle\Model\Bundle;
use Bundles\MainBundle\Entity;


/**
 * Dechet
 *
 * @ORM\Table(name="dechet")
 * @ORM\Entity(repositoryClass="Bundles\MainBundle\Entity\DechetRepository")
 */
class Dechet
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\Column(name="volume", type="float", precision=4, scale=1)
     */
    private $Volume;
    /**
     * @ORM\Column(name="datedepot", type="date")
     */
    private $dateDepot;
    /**
     * @ORM\ManyToOne(
     * targetEntity="Bundles\UserBundle\Entity\user",
     * inversedBy="Dechets",
     * )
     */
    private $Menage;
    /**
     * @ORM\ManyToOne(
     * targetEntity="Bundles\MainBundle\Entity\Conteneur",
     * inversedBy="Dechets"
     * )
     */
    private $Conteneur;
    /**
     * @ORM\ManyToOne(
     * targetEntity="Bundles\MainBundle\Entity\TypeDechet",
     * inversedBy="Dechets"
     * )
     */
    private $Type;
    /**
     * @ORM\ManyToOne(
     * targetEntity="Bundles\MainBundle\Entity\Facture",
     * inversedBy="Dechets",
     * )
     */
    private $Facture;


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

    /**
     * @return string
     */
    public function getVolume (){
        return $this->Volume;
    }

    public function setVolume ($Volume){
        $this->Volume = $Volume;
        return $this;
    }
    /**
     * @return \DateTime
     */
    public function getDate (){
        return $this->dateDepot;
    }

    public function setDate ($dateDepot){
        $this->dateDepot = $dateDepot;
        return $this;
    }
    /**
     * Get Menage
     * @return \Bundles\UserBundle\Entity\user
     */
    public function getMenage(){
        return $this->Menage;
    }

    public function setMenage ($Menage){
        $this->Menage = $Menage;
        return $this;
    }
    /**
     * Get Conteneur
     * @return \Bundles\MainBundle\Entity\Conteneur
     */
    public function getConteneur(){
        return $this->Conteneur;
    }


    public function setConteneur ($Conteneur){
        $this->Conteneur = $Conteneur;
        return $this;
    }
    /**
     * Get Type
     * @return \Bundles\MainBundle\Entity\TypeDechet
     */
    public function getType(){
        return $this->Type;
    }

    public function setType ($Type){
        $this->Type = $Type;
        return $this;
    }

    /**
     * Get Facture
     * @return \Bundles\MainBundle\Entity\Facture
     */
    public function getFacture(){
        return $this->Facture;
    }

    public function setFacture ($Facture){
        $this->Facture = $Facture;
        return $this;
    }

}

Entity Facture :

<?php

namespace Bundles\MainBundle\Entity;

use Doctrine\DBAL\Types\IntegerType;
use Doctrine\ORM\Mapping as ORM;

/**
 * Facture
 *
 * @ORM\Table(name="facture")
 * @ORM\Entity()
 */
class Facture
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\ManyToOne(
     * targetEntity="Bundles\UserBundle\Entity\user",
     * inversedBy="Factures"
     * )
     */
    private $Utilisateur;
    /**
     * @ORM\OneToMany(
     * targetEntity="Bundles\MainBundle\Entity\Dechet",
     * mappedBy="Facture",
     * cascade={"all"}
     * )
     */
    private $Dechets;
    /**
     * @ORM\Column(type="string")
     */
    private $Statut;



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

    /**
     * @ORM\Column(name="Date", type="date")
     */
    private $Date;

    /**
     * Get Utilisateur
     * @return \Bundles\UserBundle\Entity\user
     */
    public function getUtilisateur(){
        return $this->Utilisateur;
    }
    public function setUtilisateur($Utilisateur){
        $this->Utilisateur = $Utilisateur;
        return $this;
    }

    public function getDechets(){
        return $this->Dechets;
    }

    public function setDechets($Dechets){
        $this->Dechets = $Dechets;
        return $this;
    }

    public function getStatut (){
        return $this->Statut;
    }

    public function setStatut($Statut){
        $this->Statut = $Statut;
        return $this;
    }
    /**
     * @return \DateTime
     */
    public function getDate (){
        return $this->Date;
    }

    public function setDate ($Date){
        $this->Date = $Date;
        return $this;
    }
}

thanks to your reply !

I think you forget to add

$dechetSelectionne->addFacture($Facture)

Also add in your entity :

/**
 * @ORM\ManyToOne(
 * targetEntity="Bundles\MainBundle\Entity\Facture",
 * inversedBy="Dechets", cascade={"persist"}
 * )
 */
private $Facture;

First verify you have a result from getDechetByMenageAndType , assume you are below.

The main issue I see is that you specified Facture::$Dechets as OneToMany which means that Dechets is a collection (multiple) of objects.

So you would need to change the following.

namespace Bundles\MainBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\ORM\Mapping as ORM;

/**
 * Facture
 *
 * @ORM\Table(name="facture")
 * @ORM\Entity()
 */
class Facture
{
   //...
    /**
     * @var ArrayCollection|\Bundles\MainBundle\Entity\Dechet[]
     * @ORM\OneToMany(
     * targetEntity="Bundles\MainBundle\Entity\Dechet",
     * mappedBy="Facture",
     * cascade={"all"}
     * )
     */
    private $Dechets;

    //...

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

    /**
     * @return ArrayCollection|\Bundles\MainBundle\Entity\Dechet[]
     */
    public function getDechets()
    {
        return $this->Dechets;
    }

    /**
     * @param \Bundles\MainBundle\Entity\Dechet $Dechets
     */
    public function addDechet(\Bundles\MainBundle\Entity\Dechet $Dechets)
    {
        if (!$this->Dechets->contains($Dechets)) {
            $Dechets->setFacture($this); //explicitly set the facture
            $this->Dechets[] = $Dechets;
        }

        return $this;
    }

    /**
     * @param \Bundles\MainBundle\Entity\Dechet $Dechets
     */
    public function removeDechet(\Bundles\MainBundle\Entity\Dechet $Dechets)
    {
        if ($this->Dechets->contains($Dechets)) {
            $Dechets->setFacture(null); //optional if Facture is nullable
            $this->Dechets->removeElement($Dechets);
        }

        return $this;
    }
}

Then in your controller change $Facture->setDechets($DechetsSelectionne); to

$Facture->addDechet($DechetsSelectionne);

If you're submitting a collection with getDechetByMenageAndType , you will need to apply the Facture.

So you could do

foreach($DechetsSelectionne as $Dechet) {
    $Facture->addDechet($Dechet);
}

Next if the association column in the dechet database table is not Facture_id you would need to define the owning side column with a JOIN column . Assuming the column name is Facture you would use:

namespace Bundles\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * Dechet
 *
 * @ORM\Table(name="dechet")
 * @ORM\Entity(repositoryClass="Bundles\MainBundle\Entity\DechetRepository")
 */
class Dechet
{
    //...

    /**
     * @ORM\ManyToOne(
     * targetEntity="Bundles\MainBundle\Entity\Facture",
     * inversedBy="Dechets",
     * )
     * @ORM\JoinColumn(name="Facture", referencedColumnName="id", nullable=true)
     */
    private $Facture;

}

After making the changes, be sure to clear your cache

php bin/console cache:clear --no-warmup
php bin/console cache:warmup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM