rails 3.1 vs Symfony2 – my first comparison

Posted by Marcello on 16. January 2012 in coffee break, Rails

I wish all a good start in 2012!

In this new year, I have work-related to do with the Symfony2 Framework. When you ask aunt google, you will allways see forum-postings and search requests like

    symfony vs rails
    django vs rails vs symfony vs PHPcake vs your_mom_or_whatever

Because I am new to Symfony, my differentiation ability is not a reference and my claim is not a scientific approach. This is only my very first impression and I want to share it with you (comments & hints are very welcome!)

To compare the syntax of both frameworks, we create a scaffold like sourcecode (CRUD) under http://yourdomain.com/user. This will allow us to create user entries by name.
Let’s start …

Starting with Rails 3.11

In Rails 3.11 we first create our scaffold containing a model called user and a textfield called name, which is a string in our database. Rails will create a controller called users through pluralization:

rails generate scaffold user name:string

Now lets create our database

rake db:create:all

To run migrations against the currently configured database, we use rake db:migrate. This will update the database by running all of the pending migrations, creating the schema_migrations table if missing. It will also invoke the db:schema:dump task, which will update your db/schema.rb file to match the structure of your database.

rake db:migrate

Now lets have a focus on Symfony2

We first have to create a new bundle structure which will automatically be activated in our application.

php app/console generate:bundle 
--namespace=Marcello/Bundle/UserBundle --no-interaction

We have to create an “entity”, meaning a basic class that holds data. It helps fulfill the business requirement of your application. This class can’t be persisted to a database yet – it’s just a simple PHP class.

php app/console doctrine:generate:entity 
--entity="MarcelloBundle:User" --fields="name:string(255)"

Now we create a basic controller for a given entity located in our bundle. This controller allows to perform the five basic operations on a model.

php app/console generate:doctrine:crud 
--entity=MarcelloBlogBundle:User 
--format=annotation --with-write --no-interaction

Now that Doctrine knows about your database, you can have it create the database for you:

php app/console doctrine:database:create

You now have a usable Product class with mapping information so that Doctrine knows exactly how to persist it. To generate these SQL statements we just run

php app/console doctrine:schema:create

Both Frameworks at a glance

Rails 3.11:

rails generate scaffold user name:string
rake db:create:all
rake db:migrate

Symfony2:

php app/console generate:bundle --namespace=Marcello/Bundle/UserBundle --no-interaction
php app/console doctrine:generate:entity --entity="MarcelloBundle:User" --fields="name:string(255)"
php app/console generate:doctrine:crud --entity=MarcelloBlogBundle:User --format=annotation --with-write --no-interaction
php app/console doctrine:database:create
php app/console doctrine:schema:create

Symfony has also some “Magic Methods” like

$results = Doctrine::getTable('User')->findAll();

I think it’s allways worth having a look at other languages and frameworks.
Have a nice day ;-)

Tags: , , , , ,

13 Comments

  • Maerlyn says:

    Your commands are from Symfony2, but Doctrine::getTable() is from symfony1.x.

  • David says:

    Here one that used to be a PHP developer using Symfony since several years, and recently using Symfony2. But I have switched to Ruby on Rails and I have seen the sky :)

    Rails is way simpler, much useful and with better documentation. Also there are tons of resources and help for developers, as opposed to Symfony, which is something powerful but poorly documented and ridiculously verbose.

    • Marcello says:

      hello david,

      thank you very much for your comment!

      i find your comment very usefull. at the moment i have not the skillz in symfony to judge over both frameworks unbiased!

      for my job i will use symfony as a have to, but private sites will be done in rails (’cause we LOVE it ;-)

  • David says:

    I find myself thinking primarily in the tool (when using Symfony), searching information, how things work… even after several years using it as a senior developer, and after making big websites already on production. My sensation is like trying to destroy a height wall in order to accomplish the mission (build the app).

    Rails on the other way is much more pleasant to work with. I can (finally!) think on the app itself, and I feel the framework is helping me to accomplish the mission.

    With Symfony2 the difficulty is even harder. They have inserted programming structures and paradigms so that it is more confusing than before. Apparently easy tasks such as implementing both ajax/normal login functionality is undocumented and hard to implement, as the framework expects you to do it in his mysterious way.

    Another problem with Symfony is that there are very few experts to answer advanced questions in the mailing list. There are, on the other side, plenty of noobs writing tutorials and books covering what is already explained on the official documentation. The funny thing is that they are even charging money for that… basic ebooks for 12 euros, basic screencasts for 12 euros…. what the hell!! in Rails world there are plenty of free resources and high quality screencasts, some of them are paid ones, but the price is much lower for what you get, and of course MUCH useful, and real-world-oriented.

    That’s my point of view after several years in the PHP community.

  • The whole “php app/console” command easily gets tedious. Especially if you’re deep into your project and need to go back to its root or specify an absolute path.

    For that reason I have whipped together a bash script for Symfony. It’s very easy to install and then writing “sf” will suffice, from wherever you are in your project.

    Also when scaffolding my models I tend to add more than just one or two properties. And then if gets tedious defining them all in one command. Luckily, Symfony2 ships with interactive generators.

    This makes this whole process so much easier.

    sf generate:bundle
    sf doctrine:generate:entity
    sf generate:doctrine:crud
    sf doctrine:database:create
    sf doctrine:schema:create

  • Coevoittevows says:

    Hello there. I discovered your website by means of Google while searching for a similar matter, your website came up. It seems to be good. I’ve bookmarked it in my google bookmarks to visit later.

    • Micheal says:

      I agree. It would be nice if rails allowed you to iucnlde templates in your project directory that override the default templates. The original scaffolding sucked and people did not use it much. The 2nd iteration of scaffolding is much better, but still lacks easy customization. It gives you a nice base point, but there are many apps that use rails that don’t really fit the default template. BTW, welcome to Rails you kinda get used to the outdated tuts Rails has evolved so fast, that you can almost look at this as a good thing.. that means more problems have been fixed and good features added. Rails is still the best (in many cases) and the most fun framework to code, in my opinion. Its not perfect, so if you find a feature like this that you think should be added, try pushing it to the community and see if we can get it iucnlded in the codebase.

  • ... says:

    Instead of “php app/console” you could simply write ./app/console

    If you didn’t used the –no-interaction option on every single command, you could take profit of the interactive stuff too :) It saves typing –option –names everytime.

  • Marek says:

    Very unfair comparison. My first counter argument is in Symfony you can use any ORM or no ORM, in Rails ActiveRecord is by default.

  • marsbomber says:

    I’ve no experience with sf, but I’ve been a quite long time php/ZF developer before I moved onto Ruby/Rails. I have to say, after the shift, looking back at PHP, it appears to be way to verbose, way to much to write. Ruby on the other hand, is a lot concise and expressive. As for Rails, well, it’s good and bad… that’s becomes too big of a topic.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Copyright © 2011-2012 RailsZilla All rights reserved. | Imprint | Privacy