2010-02-25 30 views
0

我在Ubuntu 9.10上使用Sympelony 1.3.2和Propel ORM。一個關於如何在Symfony中嵌入表單的例子

我有一個用戶資料表,其中有連接到它的許多其他表(即user_profile.id是一個FK許多其他表

我的數據庫架構看起來是這樣的:

user_profile: 
    _attributes: { phpName: UserProfile } 
    id: ~ 
    guard_id: { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true } 
    address: { type: longvarchar, required: true } 

vehicle_type: 
    _attributes: { phpName: VehicleType } 
    id: ~ 
    name: { type: varchar(32), required: true } 


user_vehicle: 
    _attributes: { phpName: UserVehicle } 
    id: ~ 
    user_id: { type: integer, foreignTable: user_profile, foreignReference: id, required: true } 
    vehicle_type: { type: integer, foreignTable: vehicle_type, foreignReference: id, required: true } 
    license_plate:  { type: varchar(16), required: true } 


user_child: 
    _attributes: { phpName: UserChild } 
    id: ~ 
    user_id: { type: integer, foreignTable: user_profile, foreignReference: id, required: true } 
    gender: { type: boolean, required: true } 
    name:  { type: varchar(32), required: true } 

我想在用戶配置文件表單中嵌入鏈接到用戶配置文件對象的其他對象,以便當我在用戶配置文件表單上執行CRUD時,相關對象(例如UserVehicle,UserJob也同樣爲CRUD時間作爲用戶簡檔對象)

我需要一個簡單的代碼片段來展示如何:

  1. 嵌入各種相關對象(即, UserVehicle,UserChild)爲用戶配置形式
  2. 創建/更新/作爲操作正在進行刪除相關的各種對象(請注意,用戶可以分配給他們

回答

3
大於0-N車輛或兒童

你看了documentation:。

// lib/form/doctrine/ProductForm.class.php 
public function configure() 
{ 
    $subForm = new sfForm(); 
    for ($i = 0; $i < 2; $i++) 
    { 
    $productPhoto = new ProductPhoto(); 
    $productPhoto->Product = $this->getObject(); 

    $form = new ProductPhotoForm($productPhoto); 

    $subForm->embedForm($i, $form); 
    } 
    $this->embedForm('newPhotos', $subForm); 
} 

對於創建/刪除/更新的一部分,this article可能會給予一定的幫助

0

我從來沒有發現官方的方法適合我的需求,我公司開發的完全不同的方法。在我曾經工作過的公司,我們在生產中使用this new approach,發現它更有彈性和簡單。關鍵概念是「不要使用Symfony的Form類,你會發現嵌入表單可以是一個非常簡單的任務」 我希望這可以幫助你嵌入表單。

相關問題