Tuesday, May 13, 2008

CakePHP vs FUSE MVC framework - PHP

Admittedly, I'm a bit biased on the topic since FUSE has been one of my development projects for quite a while, but recently I've been working with CakePHP quite a bit. I enjoy working with Cake, but can't help but think of certain things that FUSE does (in my opinion) a bit better. I've compiled a list of a few items where I think the FUSE MVC Framework has a bit of an advantage on CakePHP.

To be fair, I've also included a few things where FUSE falls short for the moment. Before I begin, I'd also like to add that I think Cake is a great framework, and I've certainly taken cues from my work with Cake to integrate new features into FUSE.

Where FUSE Wins

1. The Query Builder object

Fetching the last 10 active employees in CAKE:
// Controller
$where_conditions[] = 'Employee.active=1';

$this->data['Employees'] = $this->Employee->findAll(
$where_conditions,
null,
null,
null,
10 );

// View
<?php foreach( $this->data['Employees'] as $employee ) {?>
<div>
<?php e($employee['Employees']['first_name'] . ' ' . $employee['Employees']['last_name'] );?>
</div>
<?php } ?>

The same thing in FUSE:
//Controller
$options['limit'] = 10;
$options['where'] = 'employees.active=1';

$this->active_employees = $this->employee->fetch_all( $options );
//View

<{ITERATOR active_employees}>
<div><{first_name}> <{last_name}></div>
<{/ITERATOR}>

The query builder is simply an object that, believe it or not, allows you to build queries. Nearly every method in FUSE that fetches data from the database (including aggregate methods like count_all()) can take a query builder object as one of its $options parameters, and it will apply the customization in that object to the query being executed.
Above is an example of the shorthand syntax for customizing queries. For more complex customization, you can use the SQLQuery object directly, as discussed in the FUSE Wiki

2. FUSE has a real templating system

Iterating through employees in CakePHP
<table>
<?php
foreach( $this->data['Results'] as $result ) {
?>
<tr><td><?php e($result['Employees']['name']);?></td></tr>
<?php
}
?>
</table>

The same thing in FUSE:
<table>
<{ITERATOR employees}>
<tr><td><{name}></td></tr>
<{/ITERATOR}>
</table>

Cake's templating system is too reminiscent of inline PHP, and is sure to send your graphics guy running in the other direction. The FUSE templating system was designed to be extremely simple but incredibly robust, not to mention made to integrate as seamlessly into HTML as possible.

3. Helper methods are easier in FUSE

To generate a dropdown box of all users by first name in Cake:
//Controller
$this->set('users', $this->User->generateList(null, null, null,'{n}.User.id', '{n}.User.first_name'));
// View:
<?php echo $form->input('SomeModel.user_id', array('class'=>'txt', 'type' => 'select')); ?>

The same thing in FUSE:
<{HTML/FormHelper::Select_all( 'User', 'id', 'first_name' )}>


Other compelling reasons to use FUSE over Cake include:
  • FUSE's User authentication and management system is much more intuitive than Cake's ACL implementation.

  • FUSE is also capable of doing nested included tables when fetching data. For example, using the 'include' option to any fetch method, one can account for the scenario where model A is linked to model B, and model B is linked to model C, but you want to select data from A, B, and C. Currently, in CakePHP, you would have to write the query manually. FUSE allows you to do:

    $options['include'] = array( 'modelA', 'modelB', 'modelB.modelC' );
    $iterator = $this->fetch_all( $options );


Where CakePHP Wins

1. CakePHP has a larger user base

Although FUSE has been in development for several years, it was only released to the general public in January of 2008. As a result, its user base is still growing, and CakePHP is a more established framework with more documentation, forums, and third party code dedicated to it. The features of each framework are generally comparable, but the size of the CakePHP community is a huge advantage over FUSE.

2. CakePHP supports database other than mySQL

FUSE 1.1 only has a database object for mySQL, but MSSQL, PostgreSQL and SQLite classes are in the works. Additionally, FUSE will not support using data from multiple databases in one project until 1.1.1.

3. CakePHP has more direct AJAX integration

FUSE supports a variety of AJAX options and makes it very easy to use JQuery, but as of version 1.1, it is not as well integrated as some of Cake's AJAX functionality.

4. Your comment here

I know I'm going to get flamed for leaving the "Where Cake wins" section so short, so I figured I'd offer the opportunity for readers to add their own favorite things about CakePHP by commenting on this entry

8 comments:

Anonymous said...

Could you please make it clear that you are a little biased about FUSE since you wrote it?

Also don't make things in CakePHP look more verbose by spreading similar code out across more lines then necessary.

Use the latest version of CakePHP for your comparisons.

Good luck with catching up ; ).

Unknown said...

There is no need for a templating system in cakephp, it's so simple that your designer might as well look up the php syntax in stead of the template system syntax.

Also, you wrote the cakephp examples on multiple lines when you wrote them on the one line for fuse. You do not need more lines in cake then in fuse.

IMO cakephp wins by far especially the 1.2 version, you've got quite some catching up to do if you want fuse to be as good as cake.

Jim Keller said...

>There is no need for a templating system in cakephp, it's so simple that your designer might as well look up the php syntax in stead of the template system syntax.

I disagree completely. Things that any HTML designer would never blink an eye at, e.g an input tag, require a hefty PHP call in Cake. Cake's inline PHP is much more convoluted than plain PHP because everything is done through function calls that generally have ordered parameters instead of named parameters.

Also, as far as the examples being on multiple lines, I wrote all the examples for both frameworks exactly as I would normally code them. Sure, you can squish everything on to one line if you want, but does anyone actually do that in practice? I'm also not trying to make the claim that less lines = better code.

Anonymous said...

Hi Jim Keller I think your opinion is respectable, but the examples you put about CakePHP actually can be made easier, and the smarty template system can be integrated in few steps to CakePHP. I'm not pretty sure if you're writing about the 1.1 version, cause the 1.2 has some new features that perhaps you missed in the 1.1

The employees' example:
$this->data = $this->Employee->find('all', array('conditions' => array('Employee.active' => 1), 'limit' => 10));

and as I said, you can easily integrate Smarty to CakePHP for the views.

for the Helper Example:

You can write this in your controller:

//Controller
$this->set('users', $this->User->find('list'));

//View
$this->input('SomeModel.user_id'); // CakePHP automatically renders it as selection box and put values for id's and you see the names (if the next field in your db is the name, otherwise simply change the value for $displayField variable in your User's model.

And about the query builder and the fetching nested models, you can visit this link http://book.cakephp.org/view/73/Retrieving-Your-Data, the complex find conditions section can let you see the real dimension of CakePHP querying.

By the way sorry for my english, I'm hispanic.

Anonymous said...

Complex WAMP install here. The rewrite system (htaccess) in FUSE worked for me out of the box.

With cake I spent days banging my head, and never got anything except "you must be stupid" out of their community.

Anonymous said...

You can use the same type of syntax for data fetching in CakePHP:

// CAKE 1.2 10 Users fetch:
$options['conditions'] = array('active'=>1);
$options['limit'] = 10;

$this->User->find('all',$options);
///////

So no difference, for a good comparison I would like to see the difference on area's like Data Validation and Data input

Unknown said...

I think the key is "compatible". Sometimes everthing needs to be rewritten, but both projects are quite young. Try to get as much compatible syntax for components or helpers in fuse and it will rock!

Wow Gold said...

WOW GOLD from randyrun. Most cheapest wow gold supplier.More than 10,000 online satisfied customers bears to the fact that we are genuine and fastest wowgold provider!