2014-01-23 41 views
0

我新建zenframework 2.我已經正確設置了zendframework 2,doctrine和zfcUser.All都能正常工作。如何從Zend Framework 2中的數據庫設置值添加表單元素

我現在的問題是,現在關於如何prepoulated一個形式,如果成員已經登錄

這是我延長zfcUser獲得的ID的會員loggged:

public function setid($id) 
    { 
     $this->id = $id; 
    } 


    public function getId() 
    { 
     if (!$this->id) { 
      $this->setid($this->zfcUserAuthentication()->getAuthService()->getIdentity()->getId()); 
     } 
     return $this->id; 
    } 

我知道要使用該Id從數據庫中獲取值,然後用這些值填充表單。

這是我的表格:

public function aboutYouAction() 
    { 

     $id = $this->getId() ; 

     $form = new CreateAboutYouForm($this->getEntityManager()); 
     $aboutYou = new AboutYou(); 
     $form->setInputFilter($aboutYou->getInputFilter()); 
     $form->bind($aboutYou); 

      if ($this->request->isPost()) 
      { 
      $form->setData($this->request->getPost()); 

      if ($form->isValid()) 
      { 
       $post = $this->request->getPost(); 
       $this->getEntityManager()->persist($aboutYou); 
       $this->getEntityManager()->flush(); 

       return $this->redirect()->toRoute('worker', array('action' => 'aboutYou')); 

      } 

      } 

      $messages=''; 


    // return array('form' => $form); 
     return new ViewModel(array('form' => $form, 'messages' => $messages)); 
    } 
+0

尤其是開始一個新的框架時,它是至關重要重要的是得到[熟悉的官方文檔(HTTP:// ZF2 .readthedocs.org/EN /最新/模塊/ zend.form.quick-的start.html#綁定一個對象)。在這種情況下,我已將您鏈接到特定部分。 – Sam

+0

爲什麼不使用$ this-> zfcUserAuthentication() - > getAuthService() - > getIdentity();獲取用戶對象,然後將其提供給表單?在您的表單中使用實體管理器服務並執行查詢似乎很腥。 – cptnk

+0

hi。我從這個例子中得到了refeence服務經理:https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md#the-controllers – andreea115

回答

0

要設置的值在表單上所有你需要做的是$form->bind($aboutYou)

bind()方法旨在利用所傳遞的實體實例,並將其映射到形式元素;這個過程被稱爲形式水合

取決於附加到表單或字段集的水合物(通常這將是DoctrineModule\Stdlib\Hydrator\DoctrineObject),這應該能夠評估AboutYou字段,包括任何實體引用/關聯,並設置相應的表單元素值。我假設這些字段之一是user

在您特定的情況下,它似乎要綁定一個新的實體(因此不會有任何的屬性設置,比如您的用戶)

$aboutYou = new AboutYou(); // Brand new entity 
$form->bind($aboutYou);  // Binding to the form without any data 

這意味着該形式正試圖設置元素的值,但提供的AboutYou類沒有要設置的數據(因爲它的新的和未通過教義加載的)和/或AboutYou類的屬性無法正確映射到表單的元素。

如果你想綁定用戶,你將需要獲取填充的實例。這可以使用原則($objectManager->find('AboutYou', $aboutYouId))或者如果您需要設置當前登錄的用戶呼叫控制器插件ZfcUser\Controller\Plugin\ZfcUserAuthentication從控制器內,並沒有其他地方

您的工作流程應該與此類似(說明目的

// Controller 
public function aboutYouAction() 
{ 
    // Get the id via posted/query/route params 
    $aboutYouId = $this->params('id', false); 

    // get the object manager 
    $objectManager = $this->getServiceLocator()->get('ObjectManager'); 

    // Fetch the populated instance 
    $aboutYou = $objectManager->find('AboutYou', $aboutYouId); 

    // here the about you entity should be populated with a user object 
    // so that if you were to call $aboutYou->getUser() it would return an user object  

    // Get the form from the service manager (rather than creating it in the controller) 
    // meaning you should create a factory service for this 
    $form = $this->getServiceLocator()->get('MyForm'); 

    // Bind the populated object to the form 
    $form->bind($aboutYou); 

    //... rest of the action such as handle edits etc 

} 
+0

嗨,AlexP,之所以我沒有使用$ this - > params('id')是我想要使用表單進行更新和新的應用程序。一世。e如果一個人沒有登錄,那麼它是一個新的表單,但是如果一個人登錄,那麼表單應該作爲更新 – andreea115

+0

從概念上講,它們是兩個不同的動作'createAction'和'editAction'。 'EditAction'你從路由中讀取'id'(或者通過'ZfcUserAuthentication'(如果它是當前用戶)和'createAction'''''''''''''''''''''''''''''''''''''''''''''''。您仍然可以使用字段集重用表單之間的功能。如果將所有當前表單元素添加到「UserFieldset」,然後將此字段集添加到兩個不同的形式「editUserForm」和「createUserForm」。 – AlexP

相關問題