我正在尝试将 Doctrine 应用于在两个表之间具有 OneToMany 关系的现有数据库:Commerce 和 Area。我从数据库生成了 yml 模式,结果如下:
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 实体具有空属性。有任何想法吗?谢谢!