我新建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));
}
尤其是開始一個新的框架時,它是至關重要重要的是得到[熟悉的官方文檔(HTTP:// ZF2 .readthedocs.org/EN /最新/模塊/ zend.form.quick-的start.html#綁定一個對象)。在這種情況下,我已將您鏈接到特定部分。 – Sam
爲什麼不使用$ this-> zfcUserAuthentication() - > getAuthService() - > getIdentity();獲取用戶對象,然後將其提供給表單?在您的表單中使用實體管理器服務並執行查詢似乎很腥。 – cptnk
hi。我從這個例子中得到了refeence服務經理:https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md#the-controllers – andreea115