Skip to content

Commit

Permalink
Fixed all the issues spotted by Ryan
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Jun 19, 2015
1 parent 20ae21b commit 4a7709b
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions cookbook/controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ in the ``Product`` entity::
Note that the type of the ``brochure`` column is ``string`` instead of ``binary``
or ``blob`` because it just stores the PDF file name instead of the file contents.

Then, add a new ``brochure`` field to the form that manages ``Product`` entities::
Then, add a new ``brochure`` field to the form that manage the ``Product`` entity::

// src/AppBundle/Form/ProductType.php
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductType extends AbstractType
{
Expand All @@ -68,7 +69,17 @@ Then, add a new ``brochure`` field to the form that manages ``Product`` entities
;
}

// ...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Product',
));
}

public function getName()
{
return 'product';
}
}

Now, update the template that renders the form to display the new ``brochure``
Expand All @@ -91,10 +102,11 @@ Finally, you need to update the code of the controller that handles the form::
// src/AppBundle/Controller/ProductController.php
namespace AppBundle\ProductController;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\Product;
use AppBundle\Form\ProductType;

class ProductController extends Controller
{
Expand All @@ -103,10 +115,13 @@ Finally, you need to update the code of the controller that handles the form::
*/
public function newAction(Request $request)
{
//...
$product = new Product();
$form = $this->createForm(new ProductType(), $product);
$form->handleRequest($request);

if ($form->isValid()) {
// $file stores the uploaded PDF file
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $product->getBrochure()

// Generate a unique name for the file before saving it
Expand All @@ -116,10 +131,11 @@ Finally, you need to update the code of the controller that handles the form::
$brochuresDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/brochures';
$file->move($brochuresDir, $fileName);

// Update the 'brochure' property to store the PDF file name instead of its contents
// Update the 'brochure' property to store the PDF file name
// instead of its contents
$product->setBrochure($filename);

// ...
// persist the $product variable or any other work...

return $this->redirect($this->generateUrl('app_product_list'));
}
Expand Down

0 comments on commit 4a7709b

Please sign in to comment.