doufu5401 2014-09-15 08:37
浏览 42
已采纳

用Doctrine实体保湿ZF2形式

I have problems Hydrating my form. When i pull my entity from the database my OrderLinePerson field is filled correctly with the connected entity. When trying to Hydrate the form it is not.

Below some example code:

<?php

namespace SenetOrder\Form;

use ZF2Core\Form\AbstractForm;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use SenetOrder\Entity\OrderLine;
use SenetOrder\Form\OrderLinePersonFieldset;

class OrderLineForm extends AbstractForm
{

    protected $organisation;

    public function __construct($name, $organisation)
    {
        $this->organisation = $organisation;

        parent::__construct($name);

        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'form-horizontal');
    }

    public function init()
    {
        $this->setHydrator(new DoctrineHydrator($this->getEntityManager()));
        $this->setObject(new OrderLine());

        $this->add([
            'name'       => 'orderLineType',
            'type'       => 'DoctrineModule\Form\Element\ObjectSelect',
            'options'    => [
                'object_manager'             => $this->getEntityManager(),
                'target_class'               => 'SenetOrder\Entity\OrderLineType',
                'property'                   => 'name',
                'disable_inarray_validator'  => true,
                'empty_option'               => '',
                'label'                      => _('Type'),
                'label_attributes'           => [
                    'class' => 'col-lg-3 control-label'
                ],
            ],
            'attributes' => [
                'class' => 'form-control',
            ],
        ]);

        $this->add(array(
            'name'       => 'start',
            'type'       => 'date',
            'options'    => array(
                'label'              => _('Start date:'),
                'label_attributes'   => array(
                    'class' => 'col-lg-3 control-label'
                ),
                'format'             => 'd-m-Y',
            ),
            'attributes' => array(
                'type'           => 'text',
                'class'          => 'form-control datepicker',
                'placeholder'    => 'dd-mm-yyyy',
                'value'          => date('d-m-Y'),
            ),
        ));

        $this->add(array(
            'name'       => 'end',
            'type'       => 'date',
            'options'    => array(
                'label'              => _('End date:'),
                'label_attributes'   => array(
                    'class' => 'col-lg-3 control-label'
                ),
                'format'             => 'd-m-Y',
            ),
            'attributes' => array(
                'type'           => 'text',
                'class'          => 'form-control datepicker',
                'placeholder'    => 'dd-mm-yyyy',
            ),
        ));

        $this->add([
            'type'       => 'SenetOrder\Form\OrderLinePersonFieldset',
        ]);

        $this->add([
            'name'       => 'submit',
            'type'       => 'submit',
            'options'    => [
                'label' => _('Save'),
            ],
            'attributes' => [
                'class' => 'btn btn-large btn-primary',
            ],
        ]);
    }

}

The connected Fieldset

namespace SenetOrder\Form;

use Zend\Form\Fieldset;
use SenetOrder\Entity\OrderLinePerson;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;

class OrderLinePersonFieldset extends Fieldset
{
    protected $serviceManager;

    public function __construct($name, $entityManager, $serviceManager, $organisation)
    {
        $this->organisation = $organisation;
        $this->serviceManager = $serviceManager;

        parent::__construct($name);

        $this->setHydrator(new DoctrineHydrator($entityManager));
        $this->setObject(new OrderLinePerson());

        $this->add([
            'name'       => 'person',
            'type'       => 'DoctrineModule\Form\Element\ObjectSelect',
            'options'    => [
                'disable_inarray_validator'  => true,
                'object_manager'             => $entityManager,
                'target_class'               => 'SenetPerson\Entity\Person',
                'label'                      => _('Person'),
                'label_attributes'           => [
                    'class' => 'col-lg-3 control-label'
                ],
                'value_options'              => $this->getOrganisationPersonOptions(),
            ],
            'attributes' => [
                'class' => 'form-control',
            ],
        ]);
    }

    protected function getOrganisationPersonOptions()
    {
        $data = [''];
        if(!is_null($this->organisation))
        {
            $service = $this->serviceManager->get('senetperson_service_person');
            $collection = $service->getBySearch(null, null, 'employee')->getResult();
            $data = [];
            foreach($collection as $person)
            {
                $data[$person->getPersonId()] = $person->parseFullname();
            }
        }
        return count($data) === 0 ? [''] : $data;
    }

}

The action how the handle the form

<?php
public function addAction()
{
    $orderLine = new OrderLine();
    $orderLine->setOrder($this->order);

    $request = $this->getRequest();
    $form = $this->getServiceLocator()->get('FormElementManager')->get('SenetOrder\Form\OrderLineForm');
    $form->bind($orderLine);

    // Handle request
    if($request->isPost())
    {
        $form->setData($request->getPost());
        if($form->isValid())
        {
            $orderLine->setCreated(new \DateTime());

            var_dump($request->getPost());
            var_dump($orderLine);die;
            die('hi there ok form');

            $this->getEntityManager()->persist($orderLine);
            $this->getEntityManager()->flush();

            $this->resultMessenger()->addSuccessMessage($this->getTranslator()->translate('The order has been successfully added'));
            return $this->redirect()->toRoute('order/view', array('orderId'=>$order->getOrderId()));
        }
        $this->resultMessenger()->addErrorMessage('There are errors in your form', $form->getMessages());
    }

    // Set breadcrumb label
    $nav = $this->getServiceLocator()->get('breadcrumb_navigation');
    $page = $nav->findByLabel(':name');
    $page->setLabel($this->order->getName());

    return new ViewModel(array(
        'form' => $form,
    ));
}

Above is the code that is executed. Below a dump of the entity after the $form->setData() is executed. Also the dump of the request object

<?php
## REQUEST
object(Zend\Stdlib\Parameters)[193]
  public 'orderLineType' => string '1' (length=1)
  public 'start' => string '01-09-2014' (length=10)
  public 'end' => string '29-09-2014' (length=10)
  public 'orderLinePerson' => 
    array (size=1)
      'person' => string '286512' (length=6)
  public 'submit' => string '' (length=0)

## ENTITY DUMP
object(SenetOrder\Entity\OrderLine)[861]
  protected 'orderLineId' => null
  protected 'order' => 
    object(SenetOrder\Entity\Order)[864]
      protected 'orderId' => string '1' (length=1)
      protected 'name' => string 'Hé leuk, orders in ZF2!' (length=24)
      protected 'description' => string 'Hé floepert, stiekem verder programmeren he!' (length=45)
      protected 'organisation' => 
        object(DoctrineORMModule\Proxy\__CG__\SenetOrganisation\Entity\Organisation)[901]
          public '__initializer__' => 
            object(Closure)[867]
              ...
          public '__cloner__' => 
            object(Closure)[868]
              ...
          public '__isInitialized__' => boolean false
          protected 'organisationId' => string '6300' (length=4)
          protected 'ownerPerson' => null
          protected 'otys' => null
          protected 'name' => null
          protected 'paymentInterval' => null
          protected 'vatNumber' => null
          protected 'debtor' => null
          protected 'invoiceByEmail' => null
          protected 'deleted' => null
          protected 'created' => null
          protected 'addressCollection' => null
          protected 'orderCollection' => null
          protected 'invoiceCollection' => null
      protected 'address' => 
        object(DoctrineORMModule\Proxy\__CG__\Application\Entity\Address)[904]
          public '__initializer__' => 
            object(Closure)[897]
              ...
          public '__cloner__' => 
            object(Closure)[896]
              ...
          public '__isInitialized__' => boolean false
          protected 'addressId' => string '56' (length=2)
          protected 'organisation' => null
          protected 'addressType' => null
          protected 'otys' => null
          protected 'address' => null
          protected 'postalcode' => null
          protected 'city' => null
          protected 'email' => null
          protected 'department' => null
          protected 'country' => null
          protected 'created' => null
          protected 'deleted' => null
      protected 'person' => 
        object(DoctrineORMModule\Proxy\__CG__\SenetPerson\Entity\Person)[933]
          public '__initializer__' => 
            object(Closure)[900]
              ...
          public '__cloner__' => 
            object(Closure)[881]
              ...
          public '__isInitialized__' => boolean false
          protected 'personId' => string '286278' (length=6)
          protected 'otys' => null
          protected 'initials' => null
          protected 'firstname' => null
          protected 'middlename' => null
          protected 'lastname' => null
          protected 'position' => null
          protected 'birthday' => null
          protected 'gender' => null
          protected 'file' => null
          protected 'created' => null
          protected 'deleted' => null
          protected 'personRoleCollection' => null
          protected 'personOrganisationCollection' => null
          protected 'personLogin' => null
          protected 'ownedOrderCollection' => null
          protected 'personOrderCollection' => null
          protected 'personHourCollection' => null
          protected 'personPhoneCollection' => null
          protected 'personEmailCollection' => null
          protected 'personContractCollection' => null
      protected 'created' => 
        object(DateTime)[862]
          public 'date' => string '2014-09-08 11:55:11.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Amsterdam' (length=16)
      protected 'orderLineCollection' => 
        object(Doctrine\ORM\PersistentCollection)[921]
          private 'snapshot' => 
            array (size=0)
              ...
          private 'owner' => 
            &object(SenetOrder\Entity\Order)[864]
          private 'association' => 
            array (size=16)
              ...
          private 'em' => 
            object(Doctrine\ORM\EntityManager)[351]
              ...
          private 'backRefFieldName' => string 'order' (length=5)
          private 'typeClass' => 
            object(Doctrine\ORM\Mapping\ClassMetadata)[932]
              ...
          private 'isDirty' => boolean false
          private 'initialized' => boolean false
          private 'coll' => 
            object(Doctrine\Common\Collections\ArrayCollection)[920]
              ...
  protected 'orderLineType' => 
    object(SenetOrder\Entity\OrderLineType)[1353]
      protected 'orderLineTypeId' => string '1' (length=1)
      protected 'name' => string 'Secondment' (length=10)
  protected 'start' => 
    object(DateTime)[1350]
      public 'date' => string '2014-09-01 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/Amsterdam' (length=16)
  protected 'end' => 
    object(DateTime)[1348]
      public 'date' => string '2014-09-29 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/Amsterdam' (length=16)
  protected 'created' => 
    object(DateTime)[1354]
      public 'date' => string '2014-09-12 12:18:38.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/Amsterdam' (length=16)
  protected 'deleted' => null
  protected 'orderLinePerson' => null

What i want to achieve is that the orderLinePerson field is filled with its object OrderLinePerson

  • 写回答

3条回答 默认 最新

  • dth8312 2014-09-23 12:21
    关注

    Missing orderLinePerson at the validationGroup

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3