If your server crashed or old files were restored by accident, or some files got corrupt (you know how that can sometimes happen), your website might experience some glitches, or worse, the dreaded “Server 500 Error”. If you are running Symfony and experiencing errors that are not typical, you might need to rebuild the bootstrap.cache.php file. From the command line, go to your Symfony root folder and paste this command into the CLI:

php ./vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php

Another issue is to comment out any of your ‘%kernal_debug%’ lines, or set to default and “%kernel_debug%’ settings in your production config.yml settings if you are operating in a production environment and restricted debugging on it.


The following updates were made to the Symfony Cheatsheet but warrant its own post here for cleaner reference:

To generate entities from an existing Symfony database, you must first have Doctrine introspect the database, then generate the entities. Watch out for your database relationships, they will probably need to be edited, getting your Doctrine table relationships mapped properly is critical.


$ php bin/console doctrine:mapping:import --force AcmeBlogBundle yml
$ php bin/console doctrine:generate:entities AcmeBlogBundle

Note above: I despise annotations in my code, I ALWAYS use YML files. Also, depending on the Symfony file structure, you may need to use ‘app/console’ instead.

Get more details regarding your current Doctrine mappings:

$ app/console doctrine:mappings:info

In Symfony, here is how you “get cookies”:

$response->headers->getCookies();

Don’t forget to include the class path:

use Symfony\Component\HttpFoundation\Cookie;

Setting cookie values in Symfony is pretty straight forward, the thing to know is that since Symfony processes so much in its front controllers you can set cookies deeper into your code than other methods where headers are sent much sooner. Since cookies can only be set before headers, this allows more flexibility within your design and coding and can also create problems like “where did that cookie go?!” unlike the olden days when you just assumed it was at the start of the new script!

$cookie = new Cookie('myCookie', 'cookieValue');
$response = new Response();
$response->headers->setCookie($cookie);

Accessing cookies directly in your TWIG template:
By the way, a separate templating engine for your views that has built-in log and ability to iterate values, etc is awesome, if you haven’t used TWIG, I highly recommend it!!!

{{ app.request.cookies.get('myCookie') }}

Here’s how to clear a cookie:

$response = new Symfony\Component\HttpFoundation\Response();
$response->headers->clearCookie('yourCookie');
$response->send();

In controller to update cookies, don’t forget to “SEND” the response.


MySQL’s “IN” query filter is very useful. Basically, if you have a list of record ID numbers, you can query them all neatly like so:

$query="SELECT field_name,field_name FROM table_name WHERE id IN (3,7,11,13,17)"

The containing comma-delimited list is a simple and neat query that can be assembled in your code quickly.

Duplicating MySQL’s “IN( )” in Doctrine is just as easy, you just have to give the “findBy” method an array based on the pattern:

array(field(s)=>array(id_val,id_val,id_val…) )

Here’s a basic example to help illustrate:

$resultsArr= $em->getRepository('repositoryName')->findBy(array('id' => array(1, 2, 3)));

I thought it was too easy, thanks to Stack Overflow for showing me the way!


Amazing, but this solution is so simple and it is no where on Stack Overflow. There’s a lot more documentation about rendering templates from a string but not TO a string, something very helpful if you want to use templating to build html output for things like email templates.

$output=$this->render('view.html.twig', array(
  'form' => $form->createView(),
  'info' => $info,
  ‘otherVals’ => $otherVals,
  ‘someArray’=> $someArray
));

var_dump($output->getContent()); exit;