Symfony:Doctrine没有更新ManyToOne

时间:2017-06-22 18:09:38

标签: symfony doctrine

当我尝试添加一个对象来自我的实体'制作' Doctrine不会更新对象' Dechet'。插件工作正常,但在我的桌子上Dechet我没有从我的对象(制作)获得我的外键:

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

感谢您的回复!

2 个答案:

答案 0 :(得分:0)

我想你忘了添加

$dechetSelectionne->addFacture($Facture)

同时添加您的实体:

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

答案 1 :(得分:0)

首先确认您的结果来自getDechetByMenageAndType,假设您在下方。

我看到的主要问题是您将Facture::$Dechets指定为OneToMany,这意味着Dechets是对象的集合(多个)。

所以你需要改变以下内容。

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

然后在您的控制器中将$Facture->setDechets($DechetsSelectionne);更改为

$Facture->addDechet($DechetsSelectionne);

如果您使用getDechetByMenageAndType提交集合,则需要申请制作。

所以你可以做到

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

接下来,如果dechet数据库表中的关联列不是Facture_id,则需要使用JOIN column定义拥有方列。假设列名为Facture,您可以使用:

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;

}

进行更改后,请务必清除缓存

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