gpt4 book ai didi

Symfony2 + Doctrine 一对多关系

转载 作者:行者123 更新时间:2023-12-01 06:18:23 28 4
gpt4 key购买 nike

我正在尝试将 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 实体具有空属性。有任何想法吗?谢谢!

最佳答案

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

关于Symfony2 + Doctrine 一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314672/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com