Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From Rails 3.2 to Cake PHP 2.0. How to do some Rails common tasks?

My intention is not to make comparisons or discuss which framework is better. I know the power from both CakePHP and Rails. I'm sure that there's a way to do similar things in both of them.

So, this is the situation: in Rails framework, I'm used to do somethings that I'd like to do on CakePHP (some of them I haven't found on the documentation).

They are:

Package management

In Rails, I have the "Gemfile" file, where I write the version of each "gem" used in the application. If I want to upgrade or downgrade, I change the version and run the "bundle update" command. How do you update plugins? Do you use tools like GIT to checkout each one to newer versions?

Migrations

When I need to change the database, I create a empty "migration" file through the console command "rails generate migration". Then, in the generated file, I add the changes, like "rename_column :users, :address, :location". After that, I run "rake db:migrate" and the database is migrated. How to do that on CakePHP?

Automated Deployment

Deployment in Rails can be made automated using the "Capistrano" ruby gem. Basically, I run "cap production deploy" in the command line. Then, based on the instructions on the "deploy.rb" file, it logs into the server(s), clone the newest version of the code from the git repository, and backups the current release, so I can rollback. It also can create symlinks for "shared" folders (like user uploads), recompile the assets, run pending migrations, install new dependencies, restart some server processes, restart the application itself, and can execute other command line tasks. Is there something similar on CakePHP?

Assets compression

In Rails, when I'm in production mode, the CSS and JS assets are automatically compiled into single files, and regenerated after each deployment. That's a native feature in Rails 3.1+. Is that possible on CakePHP?

Workers and Background Jobs

Rails can use a gem called "delayed_job" to enqueue tasks to be executed in background by "workers", like sending an e-mail after user signup, for example. How do you do that?

Namespaces for controllers

If I want to create an admin interface, or a web service (using the "api" namespace, for example), or a "mobile" namespace, I just create the respective folders on the "controllers" folder. Then, I put the controllers there and create the routes to access them. What's the best way to do that on Cake?

Access model methods from view

It seems that CakePHP return an associative array when I grab data from the database, and not the true "objects". So, I can not access the model methods. Let's suppose my UserModel class provides a method called "age" that calculates the user current age based on his birthday. In Rails, I could do this on the view: <%= @user.age %>. I need to create a view helper for doing that on Cake? Like calc_user_age($user); ?> (or something like that) ? Another situation: Let's suppose I want to get the last comment from a user, and within the comment, insert a link to the related post using the post title. In Rails I would do something like @user.comments.last.post.title to get the post title. How could I do that in Cake, without using that "recursive=3" feature that gets lots of unnecessary data?

Chaining model scopes

Let's suppose I have a model called Post. In Rails, I can create scopes on models and mix them the way I want. If I want to the get the "5 last published posts from the category Programming ordered by the most accessed", for example, I would call them this way: "Post.published.from_category("programming").most_accessed.limit(5)". If I want only the draft posts ordered by recent, integrated with pagination, I would call "Post.drafts.recent.page(2)". What is the best way to create and chain scopes on CakePHP? Build dynamically an array of conditions and send it as the parameter for "find"?

Tests

What are the testing tools adopted by the CakePHP community? I need to test the models and its methods, test the controllers and its responses and variables, and test the views content (also Javascript interaction), create fixtures, etc. I also would know if there is a way to create something like autotest, that run the tests after file saves.

Well, basically these are the points. Sorry for the long post, and for my error-prone and redundant english (i'm not a native speaker). Also, I'm not an expert. So, I can write some wrong stuff here.

Hope we can have a good conversation. Thank you!

like image 552
Bruno Dias Avatar asked Jun 06 '12 15:06

Bruno Dias


1 Answers

Package management

AFAIK there is no up to date and wide used package manager. Usually we add plugins using git. Plugins are equal to gems in RoR I think.

Edit: There is now composer which is used by CakePHP now and a lot plugins.

Migrations

Use the CakeDC migrations plugin or the build in schema shell.

Automated Deployment

We use fabric. Afaik there is no plugin for cake that will do that I'm aware of.

Asset Compression

Use this plugin.

Namespaces for controllers

Simply put them into the controller folder and use the Router to create routes for them. Same way you describe it for Rails.

Access model methods from view

Eh? You do that in Rails? I dislike it. In CakePHP you should not do that and IMO not in any MVC application.

Set the data you need in the views from the controller.

$this->set('data', $this->Model->yourMethod()); 

Workers & Backround Jobs

This can be done by writing shells.

Chaining Models

Chaining models in CakePHP is done trough associations. And yes they would be accessible like $this->User->Post->find('first') for example. You can also build complex find queries and conditions over multiple tables.

Tests

CakePHP is using phpunit for unit testing. Read this section about it in the CakePHP book.

My recommendation

Read the book: book.cakephp.org It will definitely answer most of your questions and show you how to do thinks. The documentation quality of the 2.0 version is excellent.

like image 85
floriank Avatar answered Nov 08 '22 22:11

floriank