2011-07-27 45 views
3

背景/應用預先存在的記錄中的一個對一關係創建用於使用學說ORM(1.2)

我有兩個數據庫表,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) 

任何人都可以解釋爲什麼地址第二供應商被插入到數據庫中,但實際上沒有被鏈接到供應商?

回答

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(); 
+0

感謝您的建議Curlas。對地址關係運行保存方法的第一條建議似乎不像您對Doctrine所期望的那樣有效。它與我上面的腳本三有類似的輸出;供應商對象具有地址屬性,但是這次address_id屬性爲空。 第二個建議與我目前使用的解決方案類似,是解決我的問題的方法。我正在檢查是否存在與$ supplier-> relatedExists('Address')的關係,並更改預先存在的地址或創建新地址。 – Kris

0

有一件事我喜歡學說對象時,尤其是在重複做的,如果是給我的問題是在創建新對象之前取消設置對象。試試設置

unset($supplier) 

的第一個對象分配$供應商一個新的學說對象之前。