1

我正在使用 Symfony2 2.3,我需要通过 DotrineFixturesBundle 在两个实体之间共享数据,但是 Fixtures 之间的共享对象不起作用。执行命令时 php app/console dictionary:fixtures:load --purge-with-truncate 加载数据,但关系字段为 NULL

表 Divterrigral

标识说明

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

实体数据库

<?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();
    }
}
4

1 回答 1

0

Entity Divterrigral

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

Further reading : http://doctrine-orm.readthedocs.org/en/2.0.x/reference/working-with-associations.html#transitive-persistence-cascade-operations

于 2013-07-24T00:03:39.460 回答