Zend framework2 如何在Zend Framework 2中的add form元素中设置数据库中的值

Zend framework2 如何在Zend Framework 2中的add form元素中设置数据库中的值,zend-framework2,zend-form,Zend Framework2,Zend Form,我是zenframework 2的新手。我已经正确设置了zendframework 2、条令和zfcUser。所有设置都正确 我现在的问题是,如果一个成员已经登录,如何预先设置表单 在这里,我扩展了zfcUser以获取登录成员的Id: public function setid( $id) { $this->id = $id; } public function getId() { if (!$this->id) {

我是zenframework 2的新手。我已经正确设置了zendframework 2、条令和zfcUser。所有设置都正确

我现在的问题是,如果一个成员已经登录,如何预先设置表单

在这里,我扩展了zfcUser以获取登录成员的Id:

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));
    } 

要在表单上设置值,只需
$form->bind($aboutYou)

bind()
方法用于获取传递的实体实例并将其映射到表单元素;这一过程被称为形式水合作用

根据附加到表单或字段集的属性或属性(对于条令,这通常是
DoctrineModule\Stdlib\hydrator\DoctrineObject
),它应该能够评估
关于您的
字段,包括任何实体引用/关联,并设置相应的表单元素值。我假设其中一个字段是
user

在您的特定情况下,您似乎正在绑定一个新实体(因此不会设置任何属性,例如您的用户)

这意味着表单试图设置元素的值,但提供的
AboutYou
类没有要设置的数据(因为它是新的,没有通过条令加载),和/或
AboutYou
类的属性没有正确映射到表单的元素

如果希望绑定用户,则需要获取已填充的实例。这可以使用条令(
$objectManager->find('AboutYou',$aboutYouId)
)完成,或者如果需要设置当前登录用户,则从控制器内部调用控制器插件
ZfcUser\controller\plugin\zfccuserauthentication

您的工作流程应该与此类似(仅用于说明目的


尤其是在开始一个新的框架时,获得一个新的框架是至关重要的。在本例中,我已经将您链接到了特定的部分。为什么不直接使用$this->zfcUserAuthentication()->getAuthService()->getIdentity();获取用户对象,然后将其交给表单?在表单中使用实体管理器服务并执行查询似乎有点可疑。我从这个例子中得到了refeence re Service manager:Hi-AlexP,我没有使用$this->params('id')的原因是我想将表单用于更新和新的应用程序。i、 e如果一个人没有登录,那么它是一个新表单,但是如果一个人登录,那么表单应该作为一个更新工作。实际上,它们是两个不同的操作
createAction
editAction
EditAction
从路由读取
id
(或通过
ZfcUserAuthentication
如果它是当前用户),然后
createAction
只需按原样呈现表单即可。您仍然可以通过使用字段集在表单之间重用功能。如果将所有当前表单元素添加到
UserFieldset
,然后将此字段集附加到两个不同的表单
editUserForm
createUserForm
$aboutYou = new AboutYou(); // Brand new entity
$form->bind($aboutYou);     // Binding to the form without any data
// 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

}