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!


Sometimes, you may find yourself needing to select a value or record from a database and hold that record so you can update it without any other process intervening. There are a lot of instances you may need to “lock” the record to a process to ensure no other process can “claim” it as well.

Perhaps your website has users who can “claim” a limited number of free giveaway items. Without “locking” the record, you can have duplicates “claiming” the giveaway and go over the number of free items you’re giving away. That’s just one practical example, so in MySQL you would use “SELECT FOR UPDATE” instead of just the “SELECT” statement.

If you are using Symfony and Doctrine, here’s the “SELECT FOR UPDATE” version for “DQL” (Doctrine Query Language). You set the “Lock Mode” to PESSIMISTIC_WRITE:


$query = $this->em->createQuery('SELECT …'); 
$query->setLockMode(LockMode::PESSIMISTIC_WRITE);
$em->find($class, $id, LockMode::PESSIMISTIC_WRITE);


Creating a credit card month/year input can be accomplished with a single database field but Symfony is smart enough to create multiple pull downs for each.  It is NOT smart enough to not require the programmer to exclude the “day” value.  So, what do you do?  There are many workarounds, including using some post rendering JQuery nujitsu, but that’s crazy.  Still, you have to include the “d” in the date’s format or you will get an error.

Since the day will be ignored, you have Symfony render the field then hide it with CSS.  Not as graceful as a solution you’d expect.  There are ways to create customized inputs, but something this simple I found this is a good enough solution in most cases.

Here’s how you add the credit card expiration date field to your form builder, which is usually created in the entity’s “form type”.


->add(
'ccExpirationDate',
'date',
array(
'format' =>'MMM-yyyy  d',
'years' => range(date('Y'), date('Y')+12),
'days' => array(1),
'empty_value' => array('year' => 'Select Year', 'month' => 'Select Month', 'day' => false)
)
)

// Example of outputting the form field in the TWIG template

{{  form_widget( form.payment.ccExpirationDate.day, { ‘attr’: { ‘style’: ‘display:none’ } } )  }}

Disclaimer — this was an idea used in a few different sites, not my original solution but I put it here because I am sure I’ll need to find a way to do this again.