我将 Doctrine 2 与 CodeIgniter 2 一起使用,基本上我想实现这样的目标:
$product = $this->doctrine->em->find("Entities\Product", 1)
$feature = new Entities\Feature;
$feature->setName("foo");
$product->addFeature($feature);
$this->doctrine->em->persist($product);
$this->doctrine->em->flush();
持久化特征对象时,会添加到数据库中,但 product_id 设置为 null。如何让学说自动设置外键。
我使用以下 YAML 标记通过教义命令行工具创建了我的类和表
Entities\Product:
type: entity
table: products
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 50
oneToMany:
features:
targetEntity: Feature
mappedBy: product
cascade: ["persist"]
Entities\Feature:
type: entity
table: features
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 50
manyToOne:
product:
targetEntity: Product
inversedBy: features
joinColumn:
name: product_id
referencedColumnName: id
编辑:
当然我可以通过更改 Product.php 中的 addFeature 方法来解决这个问题
public function addFeature(\Entities\Feature $features)
{
$features->setProduct($this);
$this->features[] = $features;
return $this;
}
但是由于这应该在不接触代码的情况下工作,我猜我的 YAML 标记/数据库设置有问题