2013-08-16 36 views
0

目前我在做這種事情FO用symfony,因爲我需要給用戶的基礎上配置DoctriineChoice部件形成如何訪問用戶對象的形式配置方法

$this->myForm = new MyForm(); 
$this->myForm->customConfigureMethod($this->getUser()->getGuardUser()); 

我寧願做這種事情

$this->myForm =new myCustomConfiguredForm($this->getUser()->getGuardUser()); 

與窗體實例化的定製是一部分。

任何人都知道我能做到這一點嗎?我想我可能對錶單的configure()和setup()函數之間的差異有點不清楚,所以不能清楚地考慮它。

回答

1

您將shpuld作爲選項傳遞用戶對象。這裏是一個exapmle:

class ProductForm extends BaseProductForm 
{ 
    public function configure() 
    { 
    // or use an instance variable if you need the user in an another method too 
    $user = $this->getOption('user'); 

    if (!$user instanceof sfBasicSecurityUser) 
    { 
     throw new InvalidArgumentException('A user object is required as "user" option in ' . __METHOD__); 
    } 

    // do something with the user... 
    } 

} 

$form = new ProductForm(array(), array('user' => $this->getUser()));