【问题标题】:Zf2 list elements from entities doctrine 2Zf2 列出实体学说 2 中的元素
【发布时间】:2013-12-15 18:17:23
【问题描述】:

我有一个简单的问题, 如何创建表单列表元素,例如网格或此:

[x] name | image | [button]
[ ] name | image | [button]
[x] name | image | [button]

<table>
<tr><th>checkbox</th><th>name</th><th>action</th></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
<tr><td><input type="checkbox"></td><td>name</td><td><button>OK</td></tr>
</table>

//列出数据库中的实体,数组(对象,对象,对象) //object = Application\Entity\Area

$areas = $this->getObjectManager()->getRepository('Application\Entity\Area')->findAll();

我使用了 Zend\Form\Element\Collection 表单,但我不知道如何从 db 填充集合日期,所以我有明确的表单。

我应该正确地使用它以及使用什么?

【问题讨论】:

    标签: doctrine-orm zend-framework2 zend-form2


    【解决方案1】:

    从 Doctrine 你已经得到了一个可迭代的数据类型(数组)。所以你只需要在你的视图中迭代它:

    ...
    <?php foreach($this->data as $area): ?>
    //your table row markup for a single entity
    <?php endforeach; ?>
    ...
    

    【讨论】:

      【解决方案2】:

      免责声明I have asked a similar question,没有答案。所以我也很想知道“Zend”方式,或者是否有人能够提出替代方案。

      下面的方法似乎对我有用。

      ListForm.php

      将收藏添加到您的“列表”表单中。

      /** The collection that holds each element **/
      $name = $this->getCollectionName();
      $collectionElement = new \Zend\Form\Element\Collection($name);
      $collectionElement->setOptions(array(
        'count' => 0,
        'should_create_template' => false,
        'allow_add' => true      
      ));
      $this->add($collectionElement);
      

      此集合将保留集合元素 (Zend\Form\Element\Checkbox)

      /** The element that should be added for each item **/
      $targetElement = new \Zend\Form\Element\Checkbox('id');
      $targetElement->setOptions(array(
        'use_hidden_element' => false,
        'checked_value'      => 1,
      ));
      $collectionElement->setTargetElement($targetElement);
      

      然后我添加了一些方法来允许我将ArrayCollecion 传递给表单。对于我收藏中的每个实体,我将创建一个新的$targetElement;将其检查值设置为实体的 id。

      /**
       * addItems
       *
       * Add multiple items to the collection
       * 
       * @param Doctrine\Common\Collections\Collection $items Items to add to the
       * collection
       */
      public function addItems(Collection $items)
      {
        foreach($items as $item) {
          $this->addItem($item);
        }
        return $this;
      }
      
      /**
       * addItem
       *
       * Add a sigle collection item
       * 
       * @param EntityInterface $entity The entity to add to the
       * element collection
       */
      public function addItem(EntityInterface $item)
      {
        $element = $this->createNewItem($item->getId());
        $this->get($this->getCollectionName())->add($element);
      } 
      
      /**
       * createNewItem
       *
       * Create a new collection item
       * 
       * @param  EntityInterface $entity The entity to create
       * @return \Zend\Form\ElementInterface
       */
      protected function createNewItem($id, array $options = array())
      {
        $element = clone $this->targetElement;
        $element->setOptions(array_merge($element->getOptions(), $options));
        $element->setCheckedValue($id);
      
        return $element;
      }
      

      接下来所需要做的就是将集合从控制器操作中传递给表单。

      SomeController

      public function listAction()
      {
       //....
       $users = $objectManager->getRepository('user')->findBy(array('foo' => 'bar'));
       $form = $this->getServiceLocator()->get('my_list_form');
       $form->addItems($users);
       //...
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用DoctrineModule\Form\Element\ObjectMultiCheckbox 使用数据库中的原则填充多选复选框,如本页所示:

        https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.md

        只需将实体管理器传递给表单,然后像示例中一样创建 ObjectMultiCheckbox 表单元素...

        或其他更好的-moro 自动化工作方法,如果您想使用集合,您需要对区域进行正确的映射(@orm\OneToMany 和@orm\ManyToOne)......并创建一个字段集以这里的形式...:

        http://framework.zend.com/manual/2.2/en/modules/zend.form.collections.html

        并向其他实体添加方法以添加和删除区域,如下所示:

        public function addArea(Collection $areas)
        {
            foreach ($areas as $area) {
                $area->setOtherEntity($this);
                $this->areas->add($area);
            }
        }
        
        public function removeAreas(Collection $areas)
        {
            foreach ($areas as $area) {
                $area->setOtherEntity(null);
                $this->areas->removeElement($area);
            }
        }
        

        如果您使用水合作用,这些值将在您自动选择它们时添加和删除...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-04
          • 2014-06-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-13
          相关资源
          最近更新 更多