Pages

Sunday, January 27, 2013

Scaffolding in Cakephp

Application scaffolding is a technique that allows a developer to define and create a basic application that can create, retrieve, update and delete objects.

To add scaffolding to your application, in the controller, add the $scaffold variable.

class CategoriesController extends AppController {
    var $scaffold;
}

Scaffolding a way to automatically look at what a table has and its very basic front end to check its CRUD Operations

Scaffolding automatically reads relations defined in a model

Foe example if your table has a field of tinyint attribute with length 1 then in that case cakephp will treat the same as a checkbox.

When we use a "hasMany" relation in a Model the form will show a dropdown

When we use a "has and belongs to Many" relation in a Model the form will show a multiselect list box

When we use a "belongsTo" relation in a Modelthe form will show a dropdown

The Scaffolding Structure(Form in view) shows the relation with an "Alias Name" given for the relation defined in a model.

Foe example if we have define a Table "Persons" and another table as "Horses" then we define the relation in a "Horses" model as

var $belongsTo=array('Owner'=>array('classname'=>'Person','foriegnKey'=>'owner_id'))

then Scaffolding for the  model will show the field name for owner_id as Owner.

also for a case like

var $hasAndBelongsToMany=array('Riders'=>array('className'=>'Person'));

and in Persons model we define

var $hasAndBelongsToMany=array('Horse');

we will find a multiselect list box with name "Riders" with automatically picked up names from Persons table.


3 comments:

  1. Hi

    If you want to play around with scaffolding, but in a much more flexible way - one that can be used in production too - check out my CakePHP Scaffolding / Crud plugin here:

    https://github.com/jippi/cakephp-crud

    ReplyDelete
  2. Congratulations for all the blog and the posts you share each day, it's a good point of view to keep improving the aluminum scaffolding slowly but surely, very useful

    ReplyDelete
  3. It was really a nice post and i was really impressed by reading this Ruby on Rails Online Training Bangalore

    ReplyDelete