Skip to content

Instantly share code, notes, and snippets.

View cakephp-tutorial's full-sized avatar

CakePHP Italia Tutorial cakephp-tutorial

View GitHub Profile
<?php
Configure::write('Cache.check', true);
/*ACTIVE CACHE FOR QUERIES*/
Cache::config('short', array(
'engine' => 'File',
'duration'=> '+100 minutes',
'probability'=> 100,
'path' => CACHE.'data'.DS,
'prefix' => 'cache_short_'
));
<?php
$resultset = $ModelName->find('all', array(
'recursive' => '1',
'contain' => 'Model',
'fields' => array(
'Model.field',
'OtherModel.field',
),
'conditions' => array(
<?php
public function find($type = 'first', $params = array(),$cacheParams = array()){
$doQuery =true;
if( isset($cacheParams['cache']) && !empty($cacheParams['cache'])){
$cacheConfig = null;
if(isset($cacheParams['cacheConfig']) && !empty($cacheParams['cacheConfig'])){
$cacheConfig = $cacheParams['cacheConfig'];
}
$cacheName = $this->name.'-'.$cacheParams['cache'];
$data = Cache::read($cacheName,$cacheConfig);
<!-- /app/View/Orders/add.ctp -->
<?php
echo $this->Form->create('Order');
echo $this->Form->input('company');
echo $this->Form->input('email');
echo $this->Form->input('Order.Region', array('multiple' => 'checkbox'));
echo $this->Form->end('crea');
?>
<?php
# /app/Controller/OrdersController.php
class OrdersController extends AppController {
public function add() {
if ($this->request->is(array('post', 'put')) {
$this->Order->create();
if ($this->Order->saveAll($this->request->data, array('deep' => true)) {
$this->Session->setFlash('Ordine creato con successo');
//$this->redirect(array('action' => 'view', $this->Order->id);
<?php
# /app/Model/Order.php
class Order extends AppModel {
public $hasAndBelongsToMany = array('Region');
public $validate = array(
'company' => array(
'rule' => array('notEmpty'),
'required' => true,
<?php
#/app/Model/Region.php
class Region extends AppModel {
public $displayField = 'region';
public $hasAndBelongsToMany = array('Order');
public $validate = array(
'region' => array(
'rule' => array('notEmpty'),
CREATE TABLE `regions` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`region` VARCHAR(150) NOT NULL,
`created` DATETIME DEFAULT NULL,
`modified` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `orders_regions` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`order_id` INT(11) UNSIGNED NOT NULL
`region_id` INT(11) UNSIGNED NOT NULL
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `orders` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`company` VARCHAR(150) NOT NULL,
`email` VARCHAR(150) DEFAULT NULL,
`created` DATETIME DEFAULT NULL,
`modified` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;