CakePHP vs Ruby On Rails
CakePHP is a MVC (Model-View-Controller pattern) framework that tries to mimic the way RoR (Ruby on Rails) does things. After developing a large web application on CakePHP platform, I decided to write a little bit about it and compare it the real thing. Haha, as you can tell, I am just a little bit slightly biased towards Ruby.
Being a ruby programmer, and having to make a web app that uses php+mysql I immediately started looking for something similar to Rails. There are many rails-like frameworks for php, and I ended up narrowing down my search to cakephp and symfony. Because my client specified php4 (symphony only works on php5), I went with Cake.
Scaffolder
Cake is a mature MVC framework with a large user base, excellent tutorials and API documentation. Scaffolder is a tool that generates website forms and pages for you, by looking at your Database schema. Most web MVC frameworks have a scaffolding feature and they typically generate add, edit, delete, and list functionality. This lets you quickly get things up and running.
The very first step in scaffolding is generating a “model”. A Model (M in MVC pattern) is an object representation of your sql table. Thats too vague … what does it all mean? Well, if its in PHP, then you might have a Person class (that inherits from AppModel) that is bound to “people” table.
[php]
class Person extends AppModel {
var $name = ‘Person’;
}
[/php]
Or in RoR, you have a Person class (that inherits from ActiveRecord::Base) that is bound to “people” table:
[ruby]
class Person < ActiveRecord::Base
end
[/ruby]
In the second step, you tell the scaffolder how your models are related. Some of the relations could be described as:
- A has_many B (Person has multiple email addresses)
- C belongs_to D (email address belongs to a person)
- E has_and_belongs_to_many G (E permission has and belongs to many G Roles)
- X has_one Y (wife has one husband, vise versa)
In cake, the scaffolder is in ‘cake/cake/scripts/bake.php’, and in RoR there is ’scripts/generate’. Cake’s scaffolder is more interactive, as in it asks questions while it runs. It understands relationships between models. The RoR scaffolder is also a command line tool that takes a bunch of arguements and don’t really asks any questions when it runs. The default RoR scaffolder is simplistic and doesn’t generate pulldown menus … etc, for models that are related to each other. Cake’s default scaffolder does. For instance, if you people and emails table, it will generate a pull-down menu of people inside emails add and edit screens. There are many scaffolders available for RoR, that generate pretty views and know how to handle complex relationships. RoR is way more comprehensive than cake, especially when it comes to scaffolding. One of the “alternative” scaffolders you can get for RoR is AjaxScaffold.
Model (ActiveRecord vs … nothing?)
In web MVC frameworks, a model typically is an implementation of “Active Record” pattern. Ruby’s ActiveRecord gem (package) is an object to database table mapping, kind of like DataTable in ADO.NET (yuck!). For instance, Person class is mapped to people table. You can access person.emails, delete a person with person.destroy … etc, set a person’s name with person.first_name = ‘Dmitry’, person.save.
[ruby]
# code-block way of creating a ‘person’
person = Person.new do |p|
p.first_name = “David”
p.occupation = “Code Artist”
end
# cooler example
person.categories.find_or_create_by_name(”Climbers”)
[/ruby]
Here some more examples
[ruby]
# find me by fname and state. notice that parenthesis to a function call are optional!
Person.find_by_last_name_and_city ‘Dmitry’, ‘Spokane’
# the next two lines give the same result
Person.find_by_user_name(user_name)
Person.find(:first, [”user_name = ?”, user_name])
[/ruby]
find_by_[…] is Ruby magic, because we don’t actually define a function ‘find_by_last_name_and_city()’. Its called dynamic, attribute-based finders.
In PHP you can’t do any of it. CakePHP is a very ambitious attempt to implement Rails in an inferior language. Well, actually there is a find_by() function in Cake too that supposed to mimic ActiveRecord’s, but, of course, its a hack job. To compare the two, look at RoR API’s and CakePHP’s APIs and you’ll see for yourself.
My biggest disappointment about CakePHP is its Active Record implementation: there isn’t any! In Cake, you simply retrieve a hash, modify it, then through it back to a function (or set it as $this->data) that actually saves it. So, there isn’t a real binding between an object and a database table. The ‘Person’ object in Cake is not persistant. The model class AppModel simply provides “macros” for dealing with SQL, instead of a powerful relational mapping that is found in Ruby’s ActiveRecord. Here is a snippet from php5 implementation of Model’s save function:
[php]
# save() function for php5
# …
foreach($this->data as $n => $v) {
if (isset($weHaveMulti) && isset($v[$n]) && $count > 0 && count($this->hasAndBelongsToMany) > 0) {
$joined[] = $v;
} else {
if ($n === $this->name) {
foreach($v as $x => $y) {
if ($this->hasField($x) && ($whitelist && in_array($x, $fieldList) || !$whitelist)) {
$fields[] = $x;
$values[] = $y;
if ($x == $this->primaryKey && !empty($y)) {
$newID = $y;
}
}
}
}
}
$count++;
# …
[/php]
Validation
Validation on Cake is done with regexp in ‘cake/lib/validate.php’:
[php]
/**
* Not empty.
*/
define(’VALID_NOT_EMPTY’, ‘/.+/’);
/**
* Numbers [0-9] only.
*/
define(’VALID_NUMBER’, ‘/^[0-9]+$/’);
/**
* A valid email address.
*/
define(’VALID_EMAIL’, ‘/\\A(?:^([a-z0-9][a-z0-9_\\-\\.\\+]*)@([a-z0-9][a-z0-9\\.\\-]{0,63}\\.(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$)\\z/i’);
/**
* A valid year (1000-2999).
*/
define(’VALID_YEAR’, ‘/^[12][0-9]{3}$/’);
[/php]
I needed email to be valid, not empty and unique (in RoR is validates_uniqueness_of). I was surprised not to find it.
My Conclusion
- Use the real thing: Ruby On Rails!
- If using php is a requirement, CakePHP is better then nothing (doing it “by hand” with PEAR/Smarty).
Further Reading:
- Ruby On Rails API (click on ActiveRecord)
- CakePHP API
- AjaxScaffold



August 11th, 2007 at 12:45 am
Find Any Email Address Everywhere Worldwide !…
Find Any Email Address Everywhere Worldwide !…
August 12th, 2007 at 10:28 pm
Find Any Email Address Worldwide !…
Find Any Email Address Worldwide !…
October 1st, 2007 at 1:36 pm
tessssssssssssssssst
October 5th, 2007 at 10:46 am
tterte
October 23rd, 2007 at 4:58 am
Plus De 4000 Chaînes De Télévision sur votre Ordinateur ! …
Plus De 4000 Chaînes De Télévision sur votre Ordinateur !…
November 2nd, 2007 at 6:19 pm
Hottest Motorcycle Chicks…
Hottest Motorcycle Chicks…
December 11th, 2007 at 3:19 pm
local amateur sex sex amateur father
August 30th, 2009 at 7:24 am
Докажите, что всё вот так? Сомневаюсь, каким образом прояснить данную тему.
October 28th, 2009 at 5:18 am
with such good news for us, thank you thank illumination
November 6th, 2009 at 1:41 pm
adadsada
April 29th, 2010 at 8:55 am
Great site. Keep doing., http://technorati.com/people/newdumbblondejokes new dumb blonde jokes online, qho,
May 3rd, 2010 at 11:43 am
http://bit.ly/virustotalreport