@@ -49,13 +49,14 @@ in the ``Product`` entity::
49
49
Note that the type of the ``brochure `` column is ``string `` instead of ``binary ``
50
50
or ``blob `` because it just stores the PDF file name instead of the file contents.
51
51
52
- Then, add a new ``brochure `` field to the form that manages ``Product `` entities ::
52
+ Then, add a new ``brochure `` field to the form that manage the ``Product `` entity ::
53
53
54
54
// src/AppBundle/Form/ProductType.php
55
55
namespace AppBundle\Form;
56
56
57
57
use Symfony\Component\Form\AbstractType;
58
58
use Symfony\Component\Form\FormBuilderInterface;
59
+ use Symfony\Component\OptionsResolver\OptionsResolver;
59
60
60
61
class ProductType extends AbstractType
61
62
{
@@ -68,7 +69,17 @@ Then, add a new ``brochure`` field to the form that manages ``Product`` entities
68
69
;
69
70
}
70
71
71
- // ...
72
+ public function configureOptions(OptionsResolver $resolver)
73
+ {
74
+ $resolver->setDefaults(array(
75
+ 'data_class' => 'AppBundle\Entity\Product',
76
+ ));
77
+ }
78
+
79
+ public function getName()
80
+ {
81
+ return 'product';
82
+ }
72
83
}
73
84
74
85
Now, update the template that renders the form to display the new ``brochure ``
@@ -91,10 +102,11 @@ Finally, you need to update the code of the controller that handles the form::
91
102
// src/AppBundle/Controller/ProductController.php
92
103
namespace AppBundle\ProductController;
93
104
105
+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
94
106
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
95
107
use Symfony\Component\HttpFoundation\Request;
96
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
97
108
use AppBundle\Entity\Product;
109
+ use AppBundle\Form\ProductType;
98
110
99
111
class ProductController extends Controller
100
112
{
@@ -103,10 +115,13 @@ Finally, you need to update the code of the controller that handles the form::
103
115
*/
104
116
public function newAction(Request $request)
105
117
{
106
- //...
118
+ $product = new Product();
119
+ $form = $this->createForm(new ProductType(), $product);
120
+ $form->handleRequest($request);
107
121
108
122
if ($form->isValid()) {
109
123
// $file stores the uploaded PDF file
124
+ /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
110
125
$file = $product->getBrochure()
111
126
112
127
// Generate a unique name for the file before saving it
@@ -116,10 +131,11 @@ Finally, you need to update the code of the controller that handles the form::
116
131
$brochuresDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/brochures';
117
132
$file->move($brochuresDir, $fileName);
118
133
119
- // Update the 'brochure' property to store the PDF file name instead of its contents
134
+ // Update the 'brochure' property to store the PDF file name
135
+ // instead of its contents
120
136
$product->setBrochure($filename);
121
137
122
- // ...
138
+ // persist the $product variable or any other work ...
123
139
124
140
return $this->redirect($this->generateUrl('app_product_list'));
125
141
}
0 commit comments