Roshan Book

My Tech Notebook

Cakephp


Tools

1.Bake Tool :
Use :

a) Baking an application – cake bake application name

b) Database configuration – cake bake db_config

c) Baking Model – cake bake model

d) Baking controller – cake bake controller

e) Baking view – cake bake view

f) Getting the help – cake bake help

2. Controller :

Naming Convention

a) File name – tablename_tablename2_controller.php

b) Class name in file name in camel case like – class TablenameTablename2Controller extends AppController

3. Views

Each controller function will have a separate view. And that view is called inside the layout to generate the page.

Ina controller we use the set() method to send data from your controller to your view.

$this->set(‘color’,’pink’)

Once you’ve set(), the variable can be accesses in your view.

<?php echo $color ?> displays pink

4. Associations

It comes naturally

Relationship Association Type Example
one to one hasOne A user has one profile
one to many hasMany Users in a system can have multiple recipies
many to one belongsTo A recipe belongs to a user
many to many hasAndBelongsToMany Recipes have, and belong to many tags

 

5. Layouts

Any view can have as many layouts as needed

$content_for_layout variable include view for that layout

$title_for_layout has title variable

6. Schema Tool

Uses : adding Columns, Altering Columns,Deleting Columns and Version Control

cake schema generate –fgenerate schema file for all tables with or without model with following options

Overwrite- Overwrite schema.php

Snapshot – increment schema.php

Quit

So when you select Snapshot it generates schema_2.php

Run numbered migration : cake schema run update –s2

Where 2 is the Snapshot number you wish to run

Rebuild the database : cake schema run create

Sql Dump file: cake schema dump filename.sql

 

8. Helper

HTML and FORM are default helper always available

Others helpers are Ajax, Javascript,Pagination

It is reusable between View Template, Layouts and elements

Best is when it is chosen by Controller

To enable a helper in your view

$helpers=array(‘Form’,’Html’,’Javascript’,’Time’);

You can also add helpers from within action

$this->helpers[]=’Time’

To create a custom helper

inside /app/views/helpers

class LinkHelper extends AppHelper{

function link_edit($title,$url){

//create specually formatted link

}

}

9. Element

This is like include file for the website. lives in /app/views/elements/

Filename extention is .ctp

Elements which live in generally are Ads,help boxes,navigational controls,extra menus,forms, and callouts

Calling element inside view or layout

<?php echo $this->element(‘help’); ?>

 

10. CRUD

Create row in table – $this->Post->save($this->data)

Read rows from table – $this->Post->find(‘all’)

Update row in table – $this->Post->save($this->data)

Delete row from table – $this->Post->del($id)

11. Validation

In model

var $validate=array(

‘login’=>array(‘alphaNumeric’=>array(‘rule’=>’alphaNumeric’,’required’=>true,’message’=>’alphabets and numbers only’

)

)

 

12. Behaviour

Shared between models

13. Cakephp_OOP

Placed in /project_name/vendors/directory

14. Component

Shared between controllers

15. Route

Router::connect(

‘/blog/:action/*’,

array(‘controller’=>’posts’,’action’=>’index’)

)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: