Here is a quick way to access all request variables and output them in a Symfony Twig view:

{% for request in app.request.query.all  %}
   {{ request|raw }}<br/>
{% endfor %}

 


Okay, so Sensio Lab’s Symfony is great, but those repositories are a bit limiting in their design scope.  I mean, how often do you need to access data through custom database queries and other cross referencing between database tables and not only use one table?  Of course, you’re going to have to combine database tables in even mildly complex website but Symfony’s repositories are designed to deal with one table and one “entity” referencing that table.

One way to make sure you are accessing other repositories from an entity’s repository is to overload the parent’s constructor by calling on the parent constructor first.  I actually stumbled onto this solution because I wanted to see what Symfony’s base Repository class’s constructor looked like.  Look familiar?  It passes in the entity manager ($em) and the repository’s class ($class).

protected $yourEntityRepo;

function __construct($em, $class){

   parent::__construct($em, $class);
   $this->yourEntityRepo=$this->getManager()->getRepository(YourBundle:YourEntity');

}

Connect as many repositories as you need to this way.   Keep in mind that without the parent __construct params, you can still access the entity manager and get the repositories with $this->getManager(), this is just a convenient way to do it automatically and make other repositories available within the repository if you make those repositories class variables which would then be available in any method in the repository.


A MySQL “tinyint” field, regardless of length, is mapped as a boolean in Symfony’s MySQL DBAL platform.  Good to know, so when you start typing in those Doctrine ORM mapping files in annotation or my preferred way, YML files, you won’t get a fatal error because you didn’t define the field properly.

Here’s a list of the other main MySQL field types and how they are defined in Doctrine ORM mapping files.  It can get a little confusing, like “timestamp” is also “datetime” even though “datetime” is “datetime”.  It is what it is.  Once you realize you’re just making translations for one system to talk to another, it gets easier!

This list is helpful because when you start converting your database table fields, you will need a reference, or better yet, look into “Skipper ORM” and it will take care of mappings and creating entities out of your database tables automatically for you!

protected function initializeDoctrineTypeMappings()
{
$this->doctrineTypeMapping = array(
'tinyint' => 'boolean',
'smallint' => 'smallint',
'mediumint' => 'integer',
'int' => 'integer',
'integer' => 'integer',
'bigint' => 'bigint',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'text' => 'text',
'varchar' => 'string',
'string' => 'string',
'char' => 'string',
'date' => 'date',
'datetime' => 'datetime',
'timestamp' => 'datetime',
'time' => 'time',
'float' => 'float',
'double' => 'float',
'real' => 'float',
'decimal' => 'decimal',
'numeric' => 'decimal',
'year' => 'date',
'longblob' => 'blob',
'blob' => 'blob',
'mediumblob' => 'blob',
'tinyblob' => 'blob',
'binary' => 'blob',
'varbinary' => 'blob',
'set' => 'simple_array',
);
}

 


In the newest versions of Symfony greater than 2.2 you can embed output from controller by calling it with this handy TWIG snippet:

{{ render(controller('PostBundle:Posts:recentPosts', { 'category': 'cool symfony tips' })) }}

Check out Sensio Lab’s documentation to learn more.

This is especially useful if you want to create a snippet that will deliver content independent of the controller you are calling the main content of the page — stuff around the periphery.  As long as you aren’t using any logic, this is an acceptable use of MVC and it decouples say content in a related widget on the page from the controller of the page’s main content.  This separation of concerns ensures the controller of the main content stays as lean and clean as possible!


Sometimes, a story shares more information and conveys more insights than just spoon feeding people information.  I use Doctrine ORM a lot but was curious what Symfony-based Laravel framework used as a database ORM or if it even used one at all.  After all, in Symfony you can use custom ORM packages but Doctrine works best.  A few commands on the command line can “wire up” so much of your database and basic website functions, entities and CRUD.  I’ve read and seen others use Propel in Symfony just fine, but how does Laravel work?  A good question in case you ever are pulled into a project where the team went Laravel instead of Symfony, perhaps.  It’s always healthy to stay curious in this quick changing industry.

But what about Eloquent — and what exactly is Fluent anyway?!  Well, here’s a great thread and discussion that will lead you to some answers to the these inquisitive questions by some smart, experienced Laravel experts.  Just in case you were wondering, perhaps you were just searching for “anything but procedural code systems.”  If your company is open source and you don’t use Symfony or some other PHP framework, in two or three years you probably will have to if you plan on expanding and growing your web and data infrastructures. Be warned!

– Aaron Belchamber