Archive for April, 2008

Retrieving data in CakePHP

April 23, 2008

A function to retrieve data in CakePHP is the findAll() model function.
It has the following syntax:
PLAIN TEXT
PHP:

findAll($conditions, $fields, $order, $limit, $page, $recursive);

string $conditions;

array $fields;

string $order;

int $limit;

int $page;

int $recursive;

For example to retrive all the data from a table you will use following:
PLAIN TEXT
PHP:

$this->set($var, $this->Modelname->findAll());

With the “set function” the $var value will be send to the [...]

Transaction behavior

April 8, 2008

Sample model - note the $actsAs
To change default settings do:
Download code
var $actsAs = array(’transaction’ => array(’keyName’ => ‘value’ ;) );

Model Class:
Download code <?php
class Order extends AppModel
{
var $name = ‘Order’;
var $actsAs = array(‘transaction’);
var $hasMany = array(‘OrderDetail’);
?>
Controller Class:
Download code <?php
class OrderController extends AppController {
var $name = ‘Order’;
var $uses = array(‘Order’);
function checkout() {
if(!empty($this->data)) {
$this->Order->begin(); // Start [...]

Creating Reusable Elements with requestAction

April 8, 2008

Creating reusable elements with requestAction is very simple. At the end, we can even cache the element using the new feature in 1.2.
Start of with a simple controller.
Controller Class:
Download code <?php
class PostsController extends AppController {
var $name = ‘Posts’;
function index() {
$posts = $this->Post->findAll();
if(isset($this->params['requested'])) {
return $posts;
}
$this->set(‘posts’, $posts);
}
}
?>
So, we created the Posts controller and we gave it an [...]

Multiple rules of validation per field in CakePHP 1.2

April 7, 2008

On its 1.1 release, CakePHP only allowed us to define one rule of validation per field. If we needed to specify more than one rule, we either had to use some handy extensions to CakePHP that are available, or achieve multiple validation by using the callback method beforeValidate() in our models. CakePHP 1.2 brings us [...]