Pages

Sunday, June 26, 2011

Transactions Shortcut in Cakephp

Add the following functions in app/models/AppModel

function begin()
{
$db=&ConnectionManger::getDataSource
$db->begin($this);
$this->useDbConfig;
}

function commit()
{
$db->commit($this);
}

function rollback()
{
$db->rollback($this);
}

Now in you controller
Use
$this->Model->Begin()
$this->Model->commit();
$this->Model->rollback();

Cakephp:To run an ajax on page load use

$this->Ajax->remoteFunction($options);

where $options are 'url'=>controller,action
'update'=>
'with'=>Form.SearlizeElements(new array($("BillClientId")))');

Using Pear Framework in Cakephp

1. Copy the pear in the venders directory in the root(there are 2 one in app folder as well)

If you wish to use that one copy cake/config/paths to app/config/paths and change the following line

/**
* Path to the vendors directory.
*/
if (!defined('VENDORS')) {
define('VENDORS', CAKE_CORE_INCLUDE_PATH.DS.app.DS.'vendors'.DS);
}

After copying Pear now you have the framework its time to copy components

2.copy the package needed in the pear directory

Now we will create a path for our Pear package so that Cake can easily access that

For that copy cake/config/paths.php to app/config.paths.php add an entry after venders definition as follows

"define ('PEAR',VENDORS.'Pear'.DS)";

This defines a constant for PEAR path...

Now we need to set that path in our include path for that in your boot_strap file mark an entry as

ini_set("include_path",PEAR.PATH_SEPARATOR.ini_get("include_path"));

so path is now set in "include_path".

Next we need our controller to recognize this so just before start of controller or view(as per requirement)enter the following line

App::import('Vendor','Words',array('file'=>'../vendors/Pear/Numbers/Words.php'));

That's all now you can create an object of class and use it as follows

$numberconverter=new number_words();

echo $numberconverter->towords($grandtotal);

If your view is going blank on creation of object check that the import link on top of controller or view is correct.

Cakephp:send multople elements values in observe field

$options=>array('url'=>array('controller'=>'admin','action'=>'update_list'),'update'=>'divIdToUpdate','frequency'=>'0.2','with'=>'Form.serialize elements($('elementid1','elementid2,elementid3')));

echo $this->Ajax->observefield('Schedulepart',$options);

Cakephp:Image on clear Button

$this->Html->link($this->Html->image('button-clear.gif',array('alt'=>('clear',true),'border'=>'0'),array('onclick'=>"document.form.reset();return false;",'escape'=>'false)

Cakephp:Selecting Multiple Entries from a list

use the following code

$this->Form->select('Model.name',$listarray,'selectedvalue',array('multiple'=>'multiple','size'=>'5');

CakePHP:Button Image does not appear

Button Image does not appears in fact html tags are converted to escape sequences

When ever an button image is inserted tags appear as in written instead of showing image