使用Doctrine ORM(1.2)为预先存在的记录创建一对一关系

时间:2011-07-27 17:00:30

标签: php orm doctrine relationship one-to-one

背景/应用

我有两个数据库表,supplieraddress具有一对一的关系,因为并非所有供应商都有一个地址(这只是一个较大的应用程序的简化示例)。我正在使用Doctrine ORM(1.2)和MySQL数据库。

我在向没有供应商的现有供应商添加地址时遇到了麻烦。我可以修改一个没有问题的现有供应商的地址。

以下架构和四个简单脚本显示了流程每个阶段的情况。

模式

Address:
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    town: string(300)

Supplier:
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    name: string(300)
    address_id: integer
  relations:
    Address:
      foreignType: one

脚本一:创建两个供应商,包括地址

$supplier = new Supplier();
$supplier->name = 'A supplier with an address';
$supplier->Address->town = 'A town';
$supplier->save();

$supplier = new Supplier();
$supplier->name = 'A supplier without an address';
$supplier->save();

脚本二:确认数据已保存

$supplier = Doctrine_Core::getTable('Supplier')->find(1);
var_dump($supplier->toArray());

$supplier = Doctrine_Core::getTable('Supplier')->find(2);
var_dump($supplier->toArray());

输出:

array
  'id' => string '1' (length=1)
  'name' => string 'A supplier with an address' (length=26)
  'address_id' => string '1' (length=1)
array
  'id' => string '2' (length=1)
  'name' => string 'A supplier without an address' (length=29)
  'address_id' => null

脚本三:获取并更新/创建地址

$supplier = Doctrine_Core::getTable('Supplier')->find(1);
$supplier->Address->town = 'A Different Town';
$supplier->save();
var_dump($supplier->toArray());

$supplier = Doctrine_Core::getTable('Supplier')->find(2);
$supplier->Address->town = 'A New Town';
$supplier->save();
var_dump($supplier->toArray());

输出:(注意,此时,它会建议为先前没有地址的第二个供应商创建地址)

array
  'id' => string '1' (length=1)
  'name' => string 'A supplier with an address' (length=26)
  'address_id' => string '1' (length=1)
  'Address' => 
    array
      'id' => string '1' (length=1)
      'town' => string 'A Different Town' (length=16)
array
  'id' => string '2' (length=1)
  'name' => string 'A supplier without an address' (length=29)
  'address_id' => string '2' (length=1)
  'Address' => 
    array
      'id' => string '2' (length=1)
      'town' => string 'A New Town' (length=10)

脚本四:确认更改已保存

$supplier = Doctrine_Core::getTable('Supplier')->find(1);
var_dump($supplier->toArray());

$supplier = Doctrine_Core::getTable('Supplier')->find(2);
var_dump($supplier->toArray());

$address = Doctrine_Core::getTable('Address')->find(2);
var_dump($address->toArray());

输出:

array
  'id' => string '1' (length=1)
  'name' => string 'A supplier with an address' (length=26)
  'address_id' => string '1' (length=1)
array
  'id' => string '2' (length=1)
  'name' => string 'A supplier without an address' (length=29)
  'address_id' => null
array
  'id' => string '2' (length=1)
  'town' => string 'A New Town' (length=10)

有人可以解释为什么第二个供应商的地址被插入数据库但实际上没有链接到供应商吗?

2 个答案:

答案 0 :(得分:0)

我不知道主义。我使用Propel,但我认为如果您保存供应商,则不会将地址保存在数据库中。

在保存供应商之前,您需要保存地址更改。

在伪代码中:

 $supplier=Supplier::find(2);

 $supplier->address->name="New town";

 $supplier->adress->save(); //saving de change in database. If you don't do it your change is only in the object

替代方案:

 $supplier=Supplier::find(2);

 $address= new Adress();
 $adress->name="New town";
 $adress->save();

 $supplier->adress=$adress;
 $supplier->save();

答案 1 :(得分:0)

在处理doctrine对象时,我喜欢做的一件事,特别是在迭代中,如果它给我带来问题,那就是在创建新对象之前取消设置对象。尝试取消设置

unset($supplier)

在为$ supplier分配一个新的学说对象之前的第一个对象。