2010-08-31 73 views
2

我有下面的代碼塊:幫助嵌入式BaseFormDoctrine窗體

class MerchantStoreForm extends sfForm 
{ 
    public function configure() 
    { 
    $this->disableCSRFProtection(); 

    $this->setWidgets(array(
      'brand_id' => new sfWidgetFormDoctrineChoice(array('label'=> 'Store Brand','model'=>'Brand','add_empty'=>'-Select Brand-','method'=>'getName','key_method'=>'getId','order_by'=>array('name','asc'))), 
      'newbrand' => new sfWidgetFormInputCheckbox(array('label' => 'New'),array('value'=>'Y')) 
    )); 

    $this->setValidators(array(
     'newbrand' => new sfValidatorString(array('required'=>false)),   
     'brand_id' => new sfValidatorDoctrineChoice(array('model'=>'Brand')) 
    )); 

    $brand = new Brand(); 
    $brand_form = new BrandForm(); 
    $brand_form->widgetSchema['name']->setAttribute('style','display:none'); 
    $this->embedForm('brand', $brand_form); 

    $this->getWidgetSchema()->setNameFormat('store[%s]'); 
    } 

    public function execute() 
    { 
    $form_values = $this->getValues(); 

    if($form_values['newbrand']) 
    { 
     $brand_form = $this->getEmbeddedForm('brand'); 
     $brand_form->save(); 
     $brand = $brand_form->getObject(); 
    } 
    else 
    { 
     $brand = doctrine::getTable('Brand')->findOneById($form_values['brand_id']); 
    } 

    return $brand->getId(); 
    } 
} 

兩個問題:

1)的$ brand_form->保存()不爲我工作的法寶。我得到一個500內部服務器錯誤sfValidatorErrorSchema錯誤指向我的symfony下面的代碼生成BaseBrandForm.class.php:

... 
$this->widgetSchema->setNameFormat('brand[%s]'); 
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); 
... 

這工作,而不是替代,但:

 $brand_form->updateObject($form_values['brand']); 
     $brand_form->getObject()->save(); 

這是爲什麼?

2)爲什麼我在BaseFormDoctrine嵌入窗體的對象上調用getter方法時出現未定義的方法錯誤: return $ brand-> getId();

在此先感謝您的幫助。

Sharmil

回答

1

1)BrandForm拋出一個異常,因爲它沒有任何價值。擴展sfFormObject類時直接嵌入到非對象的形式(如sfForm)不會發揮很好。

什麼MerchantStoreForm在做什麼?根據情況,它可能應該延伸sfFormObject或BrandForm應該是最高級別的形式。如果這是不可能的,你必須寫添加保存方法MerchantStoreForm調用updateObjectsave。爲了更好地理解發生的事情,請仔細閱讀sfFormObject中發生的邏輯 - 尤其值得一提的是,如果您使用的是嵌入式表單。

2)這裏沒有線索。我會看到$品牌實際上是一個實例。如果它是一條記錄,並且該記錄有一個id字段,那麼沒有理由不應該起作用。