Skip to content

Commit f6c137c

Browse files
committed
Upgrading views and documentation
1 parent 812c4e2 commit f6c137c

19 files changed

+1578
-23
lines changed

README.md

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,59 @@
1-
Work in progress...
1+
Work in progress ...
22

33
Expanded Collection List
44
========================
55

66
--picture here--
77

8-
Symfony 2 and 3 bundle for rendering related collections of entities
9-
as an expanded selectable list.
8+
Symfony 2 and 3 bundle for rendering a collection of entities as an expanded selectable list.
9+
Include some usefull form types extending the native entity form field, so all entity options
10+
could by used (like query_builder), except expanded and multiple, since all the bundle types are based in those
11+
options setted to true. Read more about entity type field in the [symfony documentation](https://getcomposer.org/doc/00-intro.md).
12+
1013

1114
**Features**
1215

13-
* Render OneToMany related collection as a selectable list.
14-
* Render ManyToMany related collection as a selectable list.
15-
* Configurable entity properties to be rendered.
16+
* [Be able to configure wich entity properties or get method should be rendered and how](Resources/doc/1-fields-configuration.md)
17+
* [Render a collection of entities as an expanded checkbox list](Resources/doc/2-expanded-onetomany.md)
18+
* [Handling ManyToMany related collection as an expanded checkbox list](Resources/doc/3-expanded-manytomany.md)
19+
* [Support for bootstrap 3 symfony theme](Resources/doc/4-bootstrap_3_example.md)
20+
21+
Installation
22+
------------
23+
24+
### Step 1: Download the Bundle
25+
26+
```bash
27+
$ composer require abdielcs/expanded-collection-bundle
28+
```
29+
30+
This command requires you to have Composer installed globally, as explained
31+
in the [Composer documentation](https://getcomposer.org/doc/00-intro.md).
32+
33+
### Step 2: Enable the Bundle
34+
35+
```php
36+
<?php
37+
// app/AppKernel.php
38+
39+
// ...
40+
class AppKernel extends Kernel
41+
{
42+
public function registerBundles()
43+
{
44+
$bundles = array(
45+
// ...
46+
new abdielcs\ExpandedCollectionBundle\ExpandedCollectionBundle(),
47+
);
48+
}
49+
50+
// ...
51+
}
52+
```
53+
54+
That's it!. Now you can start to use it.
55+
56+
License
57+
-------
58+
59+
This software is published under the [MIT License](LICENSE.md)
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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

Comments
 (0)