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.


Let’s say your website uses Symfony’s “Friends of Symfony” FOS User Bundle but you also have different internal systems and other ways users can authenticate.  You don’t want to keep pestering the user to log back in, the better solution is to get your authentication system that’s outside the FOS User Bundle to talk to it.

You take extra security precautions, perhaps white list their IP and use some other encrypted cookies values.  Here’s one way you can “refresh” their login if you send the user back to a Symfony controller that falls under the FOS User Bundle’s firewall, which you’ve defined in your Symfony’s security.yml settings file.

So if you can extract the user’s email address from the cookies already set, hopefully through encrypted means, of course, you can essentially log them back in automatically by defining the $user as the object you actually get from the userManager object.

$userManager = $this->container->get('fos_user.user_manager');
$user=$userManager>findUserByEmail($userEmail);

if($user){
   $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
   $this->get('security.context')->setToken($token);
   $this->get('session')->set('_security_main',serialize($token));
}

 

Be sure to add at the top of the controller the use of the the object:  use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

Learn more from Symfony’s documentation about Authentication.  The above is only one solution, I found the FOS User Bundle is pretty simple and flexible to work with.

Are you looking for experienced PHP, Symfony, WordPress, MySQL, LAMP, or Magento developers?  Perhaps you are looking to modernize your company’s systems, considering going open source, or launching a new e-commerce site?  Contact us and we can connect you to a network of flexible web developer freelancers that matches your organization’s needs and budget.


Go in depth with your security.yml file!  Doesn’t that sound like fun?!

Sensio Lab’s Symfony has a great reference to help developers determine the best settings to properly set up security and login behaviors, like where to redirect users after a successful login, etc:

Security Bundle Configuration

How to customize your form login

 

Click here to contact us if you are looking for a freelance web developer who specializes in PHP, Symfony, WordPress, Magento ecommerce, and MySQL.  We have access to very smart web developer experts that can fit any budget.  Ask about our special non-profit discounts!


Don’t forget to install CURL after installing PHP. The Symfony PHP framework needs it, WordPress needs it. Most websites running PHP need CURL.

I find something will need it and if your website is going to talk to other websites, you’re probably going to need CURL. Just make it a habit before rebooting after you first install your Apache or other server to run this extra line to grab CURL:

$ apt-get install php5-curl

 


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 %}