A special thanks to James Halsall for some excellent tips in tapping into Symfony events through kernel listeners.

His article explains how to create a service to trigger on certain events, this is related to the FOS User Bundle but the same pattern applies to most events in your Symfony project.  Click here to read the entire article with explanations and example code.


A strange Symfony Doctrine issue got me the other day.  I guess it’s a lack of clear understanding of how Doctrine hydrates database query results when using getArrayResult() instead of getArray().  It seems that getArrayResult() returns just the first record from your query, no matter how many records the query actually returns but getArray() returns an multi-dimensional array containing all results from the same query.

So, I needed a list of all records to display on screen, not just the first one.  All I did was change getArrayResult() to getArray() and this fixed my problem.

But why?!!  Can anyone please provide the answer?!  It’s not on Doctrine’s website, it’s not on stack.  I’ve Googled it I’ve Binged it, if I had nothing better to do maybe I’ll Duckduckgo it.  I’m afraid when I find the answer it will be so obvious that’s why I don’t get it.  I’ve read some users say it depends on what you are selecting in the query, if it’s the record index or some other value or combination of values but have not found any documentation explaining the differences clearly.  Doctrine’s horribly convoluted documentation  says this about getArrayResults():

Query#getArrayResult(): Retrieves an array graph (a nested array) that is largely interchangeable with the object graph generated by Query#getResult() for read-only purposes.

There’s money to be made — make an ORM that’s straightforward and intuitive.


In Symfony, you don’t always have to actually pass values from your controllers to your Twig views.  Twig can actually pull global and server values directly through the super global “app” variable.  Most frequently, you can access these corresponding objects like Kernel, Request, Security and Session variables like so:

{{app.kernel.cachedir}}
{{app.kernel.logdir}}
{{app.kernel.rootdir}}
 
{{app.request.basepath}}
{{app.request.baseurl}}
{{app.request.uri}}

{{app.security.token.user}}

{{app.session.locale}}

Need more Twig tips?  View our Symfony Twig Cheatsheet!

Looking for a PHP developer, a Symfony expert or WordPress web developer?  Perhaps a business system architect that specializes in scalable MySQL business system design and deployment?  We have reliable, fast and professional developers that meets your organization’s needs!  Contact us for your next web development project and schedule a free consultation today.


Here is a how you can display all your flash bag messages in a Symfony Twig view.  The “flash bag” is a special collection of session variables that hold data like a one-time message in HTML.  It is often used in e-commerce form flows when the credit card was declined, instead of further processing the payment, the rejection of the credit card appears in your TWIG view so the user can read the message and act on it.

Perhaps it will say “Credit card was denied, please check your billing ZIP code.”  or another frequent issue happens when the user fails to put in the appropriate number of credit card digits, you can have your controller put in the flash bag the message:  “Credit card requires 15 digits for AMEX, 16 digits for all other cards.”

{% for label, flashes in app.session.flashbag.all %}

    {% for flash in flashes %}

        <div class="alert alert-{{ label }}">

            {{ flash }}

        </div>

    {% endfor %}

{% endfor %}

 


Strange how obscure it is to find a clear example of how to just access or show the value of a cookie in Twig, so here it is!

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

Twig is the templating engine that powers Symfony views.  Symfony is the PHP framework from Sensio Labs.

If you are a company that uses open-source software and developers and you are not using a PHP framework, you should consider migrating your legacy “cowboy” code to a collaborative, scalable framework like Symfony before it becomes a necessity for your company.  I have overseen many migrations of systems and data over the years and am available for consultation at my business consulting site at Belchamber.us.

– Aaron Belchamber