|
| 1 | +Collection of entities as an expanded list |
| 2 | +========================================== |
| 3 | + |
| 4 | +Let's say that we need to create some shipment entity, and in the same step |
| 5 | +choose wich product to send. If we also need to show for each product more |
| 6 | +than the name, then the expanded native option of symfony form would'n be |
| 7 | +enougth. For that situation you can use the 'expanded_otm' form. |
| 8 | + |
| 9 | +An example of product entity: |
| 10 | + |
| 11 | +```php |
| 12 | +<?php |
| 13 | + |
| 14 | +namespace AppBundle\Entity; |
| 15 | + |
| 16 | +use Doctrine\ORM\Mapping as ORM; |
| 17 | + |
| 18 | +/** |
| 19 | + * Product |
| 20 | + * |
| 21 | + * @ORM\Table() |
| 22 | + * @ORM\Entity |
| 23 | + */ |
| 24 | +class Product |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var integer |
| 28 | + * |
| 29 | + * @ORM\Column(name="id", type="integer") |
| 30 | + * @ORM\Id |
| 31 | + * @ORM\GeneratedValue(strategy="AUTO") |
| 32 | + */ |
| 33 | + private $id; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var string |
| 37 | + * |
| 38 | + * @ORM\Column(name="name", type="string", length=255) |
| 39 | + */ |
| 40 | + private $name; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var string |
| 44 | + * |
| 45 | + * @ORM\Column(name="code", type="string", length=255) |
| 46 | + */ |
| 47 | + private $code; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var string |
| 51 | + * |
| 52 | + * @ORM\Column(name="price", type="decimal") |
| 53 | + */ |
| 54 | + private $price; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \stdClass |
| 58 | + * |
| 59 | + * @ORM\ManyToOne(targetEntity="Shipment", inversedBy="products") |
| 60 | + * @ORM\JoinColumn(name="id_shipment", referencedColumnName="id") |
| 61 | + */ |
| 62 | + private $shipment; |
| 63 | + |
| 64 | + // ... |
| 65 | + |
| 66 | + /** |
| 67 | + * Set shipment |
| 68 | + * |
| 69 | + * @param \AppBundle\Entity\Shipment $shipment |
| 70 | + * @return Product |
| 71 | + */ |
| 72 | + public function setShipment(\AppBundle\Entity\Shipment $shipment = null) |
| 73 | + { |
| 74 | + $this->shipment = $shipment; |
| 75 | + |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get shipment |
| 81 | + * |
| 82 | + * @return \AppBundle\Entity\Shipment |
| 83 | + */ |
| 84 | + public function getShipment() |
| 85 | + { |
| 86 | + return $this->shipment; |
| 87 | + } |
| 88 | + |
| 89 | + public function __toString() |
| 90 | + { |
| 91 | + return $this->getName(); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +And the shippment entity: |
| 97 | + |
| 98 | +```php |
| 99 | +<?php |
| 100 | + |
| 101 | +namespace AppBundle\Entity; |
| 102 | + |
| 103 | +use Doctrine\ORM\Mapping as ORM; |
| 104 | + |
| 105 | +/** |
| 106 | + * Shipment |
| 107 | + * |
| 108 | + * @ORM\Table() |
| 109 | + * @ORM\Entity |
| 110 | + */ |
| 111 | +class Shipment |
| 112 | +{ |
| 113 | + /** |
| 114 | + * @var integer |
| 115 | + * |
| 116 | + * @ORM\Column(name="id", type="integer") |
| 117 | + * @ORM\Id |
| 118 | + * @ORM\GeneratedValue(strategy="AUTO") |
| 119 | + */ |
| 120 | + private $id; |
| 121 | + |
| 122 | + /** |
| 123 | + * @var \DateTime |
| 124 | + * |
| 125 | + * @ORM\Column(name="created", type="datetime") |
| 126 | + */ |
| 127 | + private $created; |
| 128 | + |
| 129 | + /** |
| 130 | + * @var array |
| 131 | + * |
| 132 | + * @ORM\OneToMany(targetEntity="Product", mappedBy="shipment", cascade={"all"}) |
| 133 | + */ |
| 134 | + private $products; |
| 135 | + |
| 136 | + /** |
| 137 | + * Constructor |
| 138 | + */ |
| 139 | + public function __construct() |
| 140 | + { |
| 141 | + $this->products = new \Doctrine\Common\Collections\ArrayCollection(); |
| 142 | + } |
| 143 | + |
| 144 | + // ... |
| 145 | + |
| 146 | + /** |
| 147 | + * Add products |
| 148 | + * |
| 149 | + * @param \AppBundle\Entity\Product $products |
| 150 | + * @return Shipment |
| 151 | + */ |
| 152 | + public function addProduct(\AppBundle\Entity\Product $products) |
| 153 | + { |
| 154 | + $this->products[] = $products; |
| 155 | + |
| 156 | + return $this; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Remove products |
| 161 | + * |
| 162 | + * @param \AppBundle\Entity\Product $products |
| 163 | + */ |
| 164 | + public function removeProduct(\AppBundle\Entity\Product $products) |
| 165 | + { |
| 166 | + $this->products->removeElement($products); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Get products |
| 171 | + * |
| 172 | + * @return \Doctrine\Common\Collections\Collection |
| 173 | + */ |
| 174 | + public function getProducts() |
| 175 | + { |
| 176 | + return $this->products; |
| 177 | + } |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +With that in place them we can create the form type. |
| 182 | + |
| 183 | +```php |
| 184 | +<?php |
| 185 | + |
| 186 | +namespace AppBundle\Form; |
| 187 | + |
| 188 | +// ... |
| 189 | + |
| 190 | +class ShipmentType extends AbstractType |
| 191 | +{ |
| 192 | + /** |
| 193 | + * @param FormBuilderInterface $builder |
| 194 | + * @param array $options |
| 195 | + */ |
| 196 | + public function buildForm(FormBuilderInterface $builder, array $options) |
| 197 | + { |
| 198 | + $builder |
| 199 | + ->add('created') |
| 200 | + ->add('products','expanded_otm',array( |
| 201 | + 'class' => 'AppBundle\Entity\Product', |
| 202 | + 'fields' => array('name','code','price'), |
| 203 | + )) |
| 204 | + ; |
| 205 | + } |
| 206 | + |
| 207 | + // ... |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +For get the expected view also we need to set a for theme in the show and edit views: |
| 212 | + |
| 213 | +```twig |
| 214 | +{% extends '::base.html.twig' %} |
| 215 | +
|
| 216 | +{% form_theme form '@ExpandedCollection/Form/fields.html.twig' %} |
| 217 | +
|
| 218 | +{% block body -%} |
| 219 | + <h1>Shipment creation</h1> |
| 220 | +
|
| 221 | + {{ form(form) }} |
| 222 | +
|
| 223 | + <ul class="record_actions"> |
| 224 | + <li> |
| 225 | + <a href="{{ path('shipment') }}"> |
| 226 | + Back to the list |
| 227 | + </a> |
| 228 | + </li> |
| 229 | +</ul> |
| 230 | +{% endblock %} |
| 231 | +``` |
| 232 | + |
| 233 | +And that's all, the collection will be rendered as a checkbox list. |
| 234 | + |
| 235 | +Important: You could consider including the option 'by_reference' => false in the form |
| 236 | +and the next code in the Shipment class in order to handle the product's shipment reference. |
| 237 | + |
| 238 | +```php |
| 239 | +/** |
| 240 | + * Add products |
| 241 | + * |
| 242 | + * @param \AppBundle\Entity\Product $products |
| 243 | + * @return Shipment |
| 244 | + */ |
| 245 | + public function addProduct(\AppBundle\Entity\Product $products) |
| 246 | + { |
| 247 | + $products->setShipment($this); //include this line |
| 248 | + |
| 249 | + $this->products[] = $products; |
| 250 | + |
| 251 | + return $this; |
| 252 | + } |
| 253 | +``` |
0 commit comments