【问题标题】:Objects return null with DotrineFixturesBundle对象使用 DotrineFixturesBundle 返回 null
【发布时间】:2013-07-19 03:32:44
【问题描述】:

我正在使用 Symfony2 2.3,我需要通过 DotrineFixturesBundle 在两个实体之间共享数据 但是灯具之间的共享对象不起作用。 命令执行时 php 应用程序/控制台原则:fixtures:load --purge-with-truncate 数据已加载,但有关系的字段为 NULL

表分区

ID 描述

1 个描述

带有 FK Divterribase 的表

ID DIVTERRIGRAL_ID 描述

1 空描述信息

如果我通过mysql做,我可以映射关系工作。

LoadDivterrigral 类。

<?php
namespace Soint\InventarioBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Soint\InventarioBundle\Entity\Divterrigral;

class LoadDivterrigral extends AbstractFixture implements OrderedFixtureInterface{

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager){
        $ahuachapan = new Divterrigral();
        $ahuachapan->setNombre('Ahuachapan');
        $manager->persist($ahuachapan);
        $manager->flush();

        $this->addReference('dep-ahuachapan', $ahuachapan);
    }

    /**
     * {@inheritDoc}
     */
    public function getOrder(){
        return 1; // el orden en el cual serán cargados los accesorios
    }

}

LoadDivTerribase 类

<?php
namespace Soint\InventarioBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Soint\InventarioBundle\Entity\Divterribase;

class LoadDivterribase extends AbstractFixture implements OrderedFixtureInterface {
    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager){

        $ahuachapan = new Divterribase();
        $ahuachapan->setNombre('Ahuachapan');        
        $ahuachapan->setDivterrigral($this->getReference('dep-ahuachapan'));
        $manager->persist($ahuachapan);
        $manager->flush();
    }

    /**
     * {@inheritDoc}
     */
    public function getOrder(){
        return 2;
    }
}

实体分流

<?php 
namespace Soint\InventarioBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Soint\InventarioBundle\Entity\Divterribase;

/**
 * @ORM\Entity
 */
class Divterrigral {  
    /**
     * @ORM\Id 
     * @ORM\Column(type="integer") 
     * @ORM\GeneratedValue;
     */ 
    protected $id;

    /** @ORM\Column(type="string", length=100) */
    protected $nombre;

     /**
     * @ORM\OneToMany(targetEntity="Divterribase", mappedBy="divterrigral")
     */
    protected $divterribases;

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

    public function addDivterribases(Articulo $articulos){        
        $this->divterribases[] = $articulos;
    }

    public function getDivterribases(){
        return $this->divterribases;
    }    

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

    /**
     * Set nombre
     * @param string $nombre
     * @return Divterrigral
     */
    public function setNombre($nombre){
        $this->nombre = $nombre;
        return $this;
    }

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

    public function __toString(){
       return $this->getNombre();
    }
}

实体 Divterribase

<?php 
namespace Soint\InventarioBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Soint\InventarioBundle\Entity\Divterrigral;
/**
 * @ORM\Entity
 */  
class Divterribase {  
    /**
     * @ORM\Id 
     *  @ORM\Column(type="integer") 
     *  @ORM\GeneratedValue;
     */ 
    protected $id;

    /** @ORM\Column(type="string", length=100) */
    protected $nombre;

    /** 
     * @ORM\ManyToOne(targetEntity="Divterrigral", inversedBy="divterribases") 
     * @ORM\JoinColumn(name="divterrigral_id", referencedColumnName="id")
     * @return integer
     */
    protected $divterrigral;

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

    /**
     * Set nombre
     * @param string $nombre
     * @return Divterribase
     */
    public function setNombre($nombre){
        $this->nombre = $nombre;
        return $this;
    }

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

    /**
     * Set divTerriGral
     * @param Soint\InventarioBundle\Entity\Divterrigral $divTerriGral
     * @return Divterribase
     */
    public function setDivterrigral(Divterrigral $divTerriGral = null){
        $this->divTerriGral = $divTerriGral;
        return $this;
    }

    /**
     * Get divTerriGral
     * @return   Soint\InventarioBundle\Entity\Divterrigral 
     */
    public function getDivterrigral(){
        return $this->divTerriGral;
    }

    public function __toString(){
       return $this->getNombre();
    }
}

【问题讨论】:

  • 您的代码似乎正确。可能问题出在实体之间的关系上。你能发布你的实体代码吗?
  • 是的。我添加了实体代码。

标签: symfony doctrine-orm fixtures


【解决方案1】:

实体分流

/**
 * @ORM\OneToMany(targetEntity="Divterribase", mappedBy="divterrigral",cascade={"persist", "remove"})
 */
protected $divterribases;

延伸阅读:http://doctrine-orm.readthedocs.org/en/2.0.x/reference/working-with-associations.html#transitive-persistence-cascade-operations

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2018-12-16
    • 2022-01-25
    • 2021-05-17
    相关资源
    最近更新 更多