Showing posts with label cakePHP. Show all posts
Showing posts with label cakePHP. Show all posts

Tuesday, 8 February 2011
Facebook StumbleUpon Twitter Google+ Pin It

How To Customize Main Layout of Error Pages in cakePHP

To customize the contents of each cakePHP errors, you need to create the following pages in this folder /app/views/errors/
- error404.ctp
- missing_action.ctp
- missing_component_class.ctp
- missing_component_file.ctp
- missing_connection.ctp
- missing_controller.ctp
- missing_helper_class.ctp
- missing_helper_file.ctp
- missing_layout.ctp
- missing_model.ctp
- missing_scaffolddb.ctp
- missing_table.ctp
- missing_view.ctp
- private_action.ctp
- scaffold_error.ctp


But cakePHP is using /views/layouts/default.ctp as layout for these error pages like 404, missing controller, etc. To customize it, you need to create a file /app/app_error.php with the following contents:
class AppError extends ErrorHandler {
function _outputMessage($template) {
$this->controller->layout = 'error_template'; // /app/views/layouts/error_template.ctp
parent::_outputMessage($template);
}
}

And create a template file "error_template.ctp" in /app/views/layouts.

Hope this one helps.



Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

Sunday, 3 October 2010
Facebook StumbleUpon Twitter Google+ Pin It

Multiple DB in cakePHP

In order to dynamically select DB during runtime in cakePHP is try to do it in Controller or in Model.

To do it in model:


class User extends AppModel {
var $name = 'User';
var $useDbConfig = 'general_syst';

function __construct($id = false, $table = null, $ds = null) {
$this->useDbConfig = 'db1';

parent::__construct($id, $table, $ds);
}

// your code here
// ....
}


When in controller


class Users extends AppController {

function action1(){
$this->User->useDbConfig = 'db1';

$users = $this->User->find('all');

// your code here
// ....
}

}


Hope this one helps.
Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

Friday, 17 April 2009
Facebook StumbleUpon Twitter Google+ Pin It

CakePHP 1.2 Versatile Model find('list')

Have you wanted to generate a list of options for your select but wanted the display field to be generated dynamically instead of using cakePHP find('list')?
This is the case.
MODEL
User hasOne Info

User id 
username 
password 
manager_id 
Info id 
first_name 
last_name 
email 
sex

I wanted to generate a list of all Users displaying the first_name, last_name and sex. And I wanted the indexes to be generated with User.id and the display field with this format "Info.last_name, Info.first_name (Info.sex)". I have developed a function to override cakePHP $model->find.

In your app_model.php, you include this function:

class AppModel extends Model {

function find($type, $options = array()) {

switch ($type) {

case 'superlist':

if(!isset($options['format'])) {

$options['format'] = '';

for ($i=0; $i                     $options['format'] .= "%$i ";

}

$options['format'] = trim($options['format']);

}


if (isset($options['index'])){

preg_match('/(.*?)\.(.*)/', $options['index'], $match);

$index_alias = $match[1];

$index_field = $match[2];

}else{

$index_alias = $this->alias;

$index_field = 'id';

}


$options['fields'] = array_merge(array("{$index_alias}.{$index_field}"), $options['fields']);

$list = $this->find('all', $options);


$result = array();

foreach ($list as $row){

$result[$row[$index_alias][$index_field]] = $options['format'];


foreach ($options['fields'] as $index=>$field){

preg_match('/(.*?)\.(.*)/', $field, $match);

$field_alias = $match[1];

$field_field = $match[2];


$result[$row[$index_alias][$index_field]] = 

str_replace('%'.$index, $row[$field_alias][$field_field], 

$result[$row[$index_alias][$index_field]]);

}

}

return $result;

break;                       

default:              

return parent::find($type, $options);

break;

}

}

}

This is how you call the generate list function in your controller action.

// if you have not declared hasOne Info in User model, this is needed in order for User model generate Info model with it and you can pass condition on Info fields

$this->User->bindModel(

array(

'hasOne' => array('Info')

)

);

$users = $this->User->find('superlist',

array(

'format' => '%3, %2 (%1)',

'index' => 'User.id', // --> this is the default so you may not pass this option

'fields' =>

array(

'Info.sex', // --> %1

'Info.first_name', // --> %2

'Info.last_name', // --> %3

),

'order' => "Info.first_name, Info.last_name", // -- if you want to sort the option

'condition' => "User.manager_id=2 AND Info.lastname='Smith'", // --> if you need some filtering

)

);

$this->set(compact('users'));

$users array will contain the following structure:

array(

'User.id1' => 'Info.last_name1, Info.first_name1 (Info.sex1)',

'User.id2' => 'Info.last_name2, Info.first_name2 (Info.sex2)',

'User.id3' => 'Info.last_name3, Info.first_name3 (Info.sex3)',

.

.

.  

'User.idN' => 'Info.last_nameN, Info.first_nameN (Info.sexN)',

)

Hope this post can help you in your future cakePHP 1.2 development.

Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

Friday, 13 February 2009
Facebook StumbleUpon Twitter Google+ Pin It

CakePHP Routing With Parameters

Here is an example to illustrate routing with parameters.

/config/routes.php
Router::connect(
    '/:controller/:year/:month/:day',
    array('action' => 'index', 'day' => null),
    array(
        'year' => '[12][0-9]{3}',
        'month' => '(0[1-9]|1[012])',
        'day' => '(0[1-9]|[12][0-9]|3[01])'
    )
);


Parameters can be access like this:

/controllers/controller.php
function index(){
    // params is stored in $this->params
    $year = $this->params['year'];
    $month = $this->params['month'];
    $day = $this->params['day'];
    
    //
}

Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

Thursday, 12 February 2009
Facebook StumbleUpon Twitter Google+ Pin It

How to Route Admin Actions in CakePHP 1.2

This is how to setup the routing:

Router::connect('/admin/:controller/:action/*', array('admin'=>'true','prefix'=>'admin', 'controller'=>'controller', 'action'=>'action'));



Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

Indenting Tree List in Form Input Select using ' ' as spacer

This is the scenario in cakePHP 1.2.

In your controller, you have an action:

function add() {
 ... 
 $categories = $this->Listing->Category->generatetreelist(null, null, null, ' ');
 $this->set(compact('categories','keywords','metros','states'));
}

where ' ' is your spacer.

Your view will have something like this:
...
echo $form->input('Category');
...

The above combination will display a drop down menu with the '&nsbp;' printed as '&nbsp' and not as a space. So how would you do it in cakePHP 1.2?

The key here is in the view file.

instead of the above statement, you should have this:
...
echo $form->input('Category', array('escape' => false));
...
Hope this one helps.


Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

Monday, 26 January 2009
Facebook StumbleUpon Twitter Google+ Pin It

Empty Options for cakePHP 1.2

$options['empty'], if set to true, forces the input to remain empty.

When passed to a select list, this creates a blank option with an empty value in your drop down list. If you want to have a empty value with text displayed instead of just a blank option, pass in a string to empty.

<?php echo $form->input('field', array('options' => array(1,2,3,4,5), 'empty' => '(choose one)')); ?>


Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

How to Include Javascript File in Cake PHP

If you want to use a Javascript file, you usually reference it in the head section of your layout:

<head>
    <?php echo $javascript->link('script.js'); ?>
</head>

But sometimes you want to use a certain Javascript file only in one view. Then it doesn’t make sense to reference it in the layout. You could take the PHP statement from above and place it in your view, that will work. But it is not a clean solution as the Javascript reference is placed somewhere in the middle of the generated HTML code.

Fortunately, CakePHP 1.2 allows you to define a reference to a Javascript file which is then added to the head section of the generated HTML code. For this purpose the variable $scripts_for_layout has to be added to the layout:

<head>
    <?php echo $scripts_for_layout; ?>
</head>

In the view you can now link to the Javascript file with:

$javascript->link('script.js', false);

Notice the second parameter. Set it to false to tell cake to put the reference in $scripts_for_layout variable in the layout file.



Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986