2012-07-03 164 views
3

我想将原则应用于两个表之间具有OneToMany关系的现有数据库:Commerce和Area。 我产生从数据库中产生以下的YML模式:Symfony2 + Doctrine一对多关系

Area: 
    type: entity 
    table: area 
    fields: 
    id: 
     id: true 
     type: integer 
     unsigned: false 
     nullable: false 
     generator: 
     strategy: IDENTITY 
    name: 
     type: string 
     length: 255 
     fixed: false 
     nullable: true 
    lifecycleCallbacks: { } 

Commerce: 
    type: entity 
    table: commerce 
    fields: 
    id: 
     id: true 
     type: integer 
     unsigned: false 
     nullable: false 
     generator: 
     strategy: IDENTITY 
    name: 
     type: string 
     length: 255 
     fixed: false 
     nullable: true 
    manyToOne: 
    area: 
     targetEntity: Area 
     cascade: { } 
     mappedBy: null 
     inversedBy: null 
     joinColumns: 
     area_id: 
      referencedColumnName: id 
     orphanRemoval: false 
    lifecycleCallbacks: { } 

从这个模式我产生的实体:

use Doctrine\ORM\Mapping as ORM; 

/** 
* Model\EntitiesBundle\Entity\Area 
* 
* @ORM\Table(name="area") 
* @ORM\Entity 
*/ 
class Area 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

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


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

} 


use Doctrine\ORM\Mapping as ORM; 

/** 
* Model\EntitiesBundle\Entity\Commerce 
* 
* @ORM\Table(name="commerce") 
* @ORM\Entity 
*/ 
class Commerce 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

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

    /** 
    * @var Area 
    * 
    * @ORM\ManyToOne(targetEntity="Area") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="area_id", referencedColumnName="id") 
    * }) 
    */ 
    private $area; 


    /** 
    * @return \Model\EntitiesBundle\Entity\Area 
    */ 
    public function getArea() 
    { 
     return $this->area; 
    } 

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

我的问题是当我尝试:

$commerce = $em->getRepository('ModelEntitiesBundle:Commerces') 
       ->find($id); 
echo $commerce->getArea()->getName(); 

Area实体具有空属性。 有什么建议吗?谢谢!

回答

0

我解决了这个问题。这是其他地方,我有两个实体经理,我需要的不是默认的。

相关问题