Symfony Live Berlin 2014

In October I went to the SymfonyLive in Berlin, I was pleasantly surprised how high the niveau of the speeches was. It was my first time there, in the years before I was only attending some general fairs like the Internet World or the TYPO3-Bootcamp, where I found some lightweighted CSS-talks :)

But here at SimfonyLive my tech-heart jumped higher, because all the topics seemed quite interesting to me when I read the program. After all there were some better speakers and some worse (as always), but these were my favourite ones:

Thorsten Rinne https://twitter.com/ThorstenRinne
He talked about a whole refactoring and redesign of the code and infrastructure of Yatego and how they used Elastic Search for that, very interesting talk

Benjamin Eberlei
http://qafoo.com/resources/presentations/symfony_live_berlin_2014_2014/feature_flags_with_symfony.html
How does Facebook roll out or test certain features to a small group of people? Tadaaaah, it’s Feature Flags!

Nils Adermann, creator of composer (thanks for that, you’re a genius)
http://naderman.de/slippy/src/?file=2014-10-31-Efficient-Use-of-Microservices.html
Nils presented a nice way to asynchronically work on datastreams. Wow, I didn’t know you could go so far with PHP. For example a curl-call to a 1-mio-item-json which never has to be persisted in whole

Dustin Whittle: Scaling PHP
https://speakerdeck.com/dustinwhittle/scaling-php
MemCached, Workers, Doctrine-Cache, http-cache: a solid roundtrip to all the performance-approaches in the PHP-world.

 

 

Useful tips for symfony2/doctrine optimization

In the LinkedIn-symfony-group I stumbled upon a very useful link:

http://labs.octivi.com/mastering-symfony2-performance-doctrine/

This is a must-read for all doctrine-beginners, I wish i woud have read it when I started… Here are the points in short:

  • use the Symfony Profiler toolbar
  • avoid object hydration
  • use IDs for referencing rather than whole object
  • use update-statement
  • use lazy collections
  • never load entities in a loop

More infos can be found in the article.

Truncate text in Twig & Symfony 2

In the core symfony / twig do not have a simple truncate-text-function, like substr() in PHP. Therefor you have to activate the Twig-Text-Extension:

Now you can easily truncate as you like:

If you want to preserve whole words, you have to set the 2nd parameter:

With the 3rd parameter you can override the default seperator ‘…':

 

Thanks Scott :)

Store Symfony-Sessions in the Database

On my recent project I used cloudhosting www.cloudcontrol.com for easy scaling. Unfortunately they don’t support synchronized filestorage, so when you store a session on one machine, on the next request it is gone :(

There is a quite simple workaround, just store your sessions in the database:
http://symfony.com/doc/current/cookbook/configuration/pdo_session_storage.html

The drawback is a (possibly) pretty huge database, so make sure you have some kind of garbage collector that cleans the entries from time to time.

PHP-Traits in Symfony: Add timestamps and more to tables magically

Say you want to add a logging preprocessor to some of your classes. Either all of these classes have to inherit some kind of logger-class, or you have to copy the logging-call multiple times. Not anymore! PHP 5.4.0 introduced Traits, a wonderful way to inject code into your classes without using inheritance.

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way.

Some guys in the Symfony-community prepares some great traits:

  • Timestampable
  • Loggable
  • Translatable
  • Sortable
  • Tree

Go check them out:

https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/timestampable.md#traits

Trailing slash in Symfony route

I found several approaches to get rid of the annoying 404-error when you open a route with a trailing-slash:

www.symfonysite.com/route
>> ok

www.symfonysite.com/route/
>> Error: 404-not found

The easiest solution is to add a RewriteRule to your .htaccess:

Now all requests with trailing-slash are redirected to the one without trailing-slash.

Notice that this only works as long as you do not have any routes which want require a trailing slash, because this would lead to an redirect loop:

 

Table inheritance with Doctrine

Currently I am programming a web-application with Symfony. In this app we got different kind of users: patients, doctors, experts, admins and so on. All of these users share some attributes (username, password, email), but some attributes only apply to several usertypes. A doctor for example has a departement, an expert a phone-number etc.

In the object oriented world of PHP this is an easy task: Patient extends User. But thanks to the magic of Doctrine this can be used within MySQL as well!

Daniel Barsotti wrote an excellent post about it: http://blog.liip.ch/archive/2012/03/27/table-inheritance-with-doctrine.html

Works like a charm!