Zf2 list elements from entities doctrine 2

1k views Asked by At

I have simple question, how can I create form list elements, something like grid or this:

[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>

//list entities from db, array(object,object,object) //object = Application\Entity\Area

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

I used in form Zend\Form\Element\Collection but I don't know how populate collection date from db, so I had clear form.

I should do it properly and what to use?

3

There are 3 answers

0
asdflair On

From Doctrine you already get an iterable datatype (array). So you only need to iterate it in your view:

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

You can populate multi-select checkbox using doctrine from the database using DoctrineModule\Form\Element\ObjectMultiCheckbox as in this page:

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

simply you need to pass the entity manager to the form, and then do same as in the example you can create ObjectMultiCheckbox form element...

or the other better -moro automated work- method, if you want to use the collection you need to do the mapping right (@orm\OneToMany and @orm\ManyToOne) with the area... and the create a fieldset in the form as in here...:

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

and add methods to the other entity to add and remove the areas as this:

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

By this if you use the hydration the values will be added and removed as you select them automatically...

0
AlexP On

Disclaimer: I have asked a similar question, with no answer. So I would also be keen to know the 'Zend' way or if anyone is able to suggest an alternative.

The approach below seems to work for me.

ListForm.php

Add a collection to your 'list' form.

/** 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);

This collection will hold out collection element (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);

Then I add a few methods to allow me to pass an ArrayCollecion to the form. For each entity in my collection I will create a new $targetElement; setting its it's checked value to the id of the entity.

/**
 * 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;
}

All that is then needed is to pass the collection to the form from within the controller action.

SomeController

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