Thursday, May 13, 2010

CAKEPHP

beforeRender()
Suppose you needed an array of colors to be available to every view rendered by your controller but you don't want to have to define this data in every action. Using the beforeRender() callback will allow you to do this:
function beforeRender() {
$this->set('colors',array('red','blue','green');
}

This would make $colors accessible in every view rendered by that controller. beforeRender() is called after the controller logic and just before a view is rendered.


beforeFilter()
If you define a beforeFilter in your controller, it will be executed before each action. Sometimes, that behaviour is not desired, and you want to exclude an action from the application of the beforeFilter. The following simple example gives you an idea of how to accomplish that:


// app/controllers/users_controller.php
class UsersController extends AppController
{
function beforeFilter()
{
if ($this->action != 'login')
{
// execute beforeFilter logic
}
}

function login()
{
// do login
}

function edit($id)
{
// do edit
}

function delete($id)
{
// do delete
}
}


Containable behavior:
To use the new behavior, you can add it to the $actsAs property of your model:
class Post extends AppModel
{
var $actsAs = array('Containable');
}

For example, Let's say that Post hasMany Comment, and Post hasAndBelongsToMany Tag, so to get only the post-related information, you can do the following:
$this->Post->contain();
$this->Post->find('all');

If we wanted to fetch all posts and their related tags (without any comment information), we'd try something like this:

$this->Post->contain('Tag');
$this->Post->find('all');

Containable also goes a step deeper: you can filter the data of the associated models.
If you are interested in the posts and the names of the comment authors — and nothing else — you could do something like the following:
$this->Post->contain('Comment.author');
$this->Post->find('all');


find('first', $params)
'first' is the default find type, and will return one result.

$alsoLastCreated = $this->Article->find('first', array('order' => array('Article.created DESC')));
$specificallyThisOne = $this->Article->find('first', array('conditions' => array('Article.id' => 1)));


find('count', $params)
find('count', $params) returns an integer value. Below are a couple of simple (controller code) examples:


function some_function() {
$total = $this->Article->find('count');
$pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));
$authors = $this->Article->User->find('count');
$publishedAuthors = $this->Article->find('count', array('fields' => 'COUNT(DISTINCT Article.user_id) as count','conditions' => array('Article.status !=' => 'pending')));
}


find('all', $params)
find('all') returns an array of (potentially multiple) results.

function some_function() {
$allArticles = $this->Article->find('all');
$pending = $this->Article->find('all', array('conditions' => array('Article.status' => 'pending')));
$allAuthors = $this->Article->User->find('all');
$allPublishedAuthors = $this->Article->User->find('all', array('conditions' => array('Article.status !=' => 'pending')));
}


updateAll(array $fields, array $conditions)
Updates many records in a single call. Records to be updated are identified by the $conditions array, and fields to be updated, along with their values, are identified by the $fields array.

For example, to approve all bakers who have been members for over a year, the update call might look something like:

$this_year = date('Y-m-d H:i:s', strtotime('-1 year'));
$this->Baker->updateAll(array('Baker.approved' => true),array('Baker.created <=' => $this_year));


deleteAll(mixed $conditions, $cascade = true, $callbacks = false)
$conditions_itdays = array('ItineraryDay.itinerary_id' => $arrGetItineraryInfoarr['Itinerary']['id']);
$this->ItineraryDay->deleteAll($conditions_itdays);


hasMany
User hasMany Comment (user is main table and comment is table having user_id as foreign key (Comment.user_id))

We can define the hasMany association in our User model at /app/models/user.php using the string syntax as follows:

class User extends AppModel {
var $name = 'User';
var $hasMany = 'Comment';
}
?>


hasOne
Let’s set up a User model with a hasOne relationship to a Profile model.

User hasOne Profile (means user is main table and profiles is table having field user_id (profiles.user_id))

The User model file will be saved in /app/models/user.php

class User extends AppModel {
var $name = 'User';
var $hasOne = 'Profile';
}
?>


belongsTo
Profile belongsTo User (profiles.user_id means user is main table and profile table has user_id field)

We can define the belongsTo association in our Profile model at /app/models/profile.php using the string syntax as follows:

class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = 'User';
}
?>