Last active
August 29, 2015 14:10
-
-
Save khoand0000/a6266b4a2856c49a8b1a to your computer and use it in GitHub Desktop.
cakephp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PostsController extends AppController { | |
public $uses = array('Post', 'Recipe', 'User'); // use more models: Recipe, User | |
public $helpers = array('Js'); | |
public $components = array('RequestHandler'); | |
public function my_action() { | |
$this->render('custom_file'); // render app/View/Posts/custom_file.ctp instead of app/View/Posts/my_action.ctp | |
// Render the element in /View/Elements/ajaxreturn.ctp | |
$this->render('/Elements/ajaxreturn'); | |
} | |
} | |
// load model | |
$this->loadModel('Article'); | |
$recentArticles = $this->Article->find( | |
'all', | |
array('limit' => 5, 'order' => 'Article.created DESC') | |
); | |
$this->loadModel('User', 2); | |
$user = $this->User->read(); | |
// redirect | |
return $this->redirect( | |
array('controller' => 'orders', 'action' => 'thanks') | |
); | |
$this->redirect('/orders/thanks'); | |
$this->redirect('http://www.example.com'); | |
$this->redirect(array('action' => 'edit', $id)); | |
// render json | |
$this->set('response', $response); | |
$this->set('_serialize', 'response'); | |
// render a tag | |
$view = new View($this, false); | |
$view->Html->link(__('Edit'), array('action' => 'edit', $item['RightPromotion2Rule']['id']), array('class' => 'btn btn-primary edit')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://book.cakephp.org/2.0/en/models/retrieving-your-data.html | |
find(string $type = 'first | all | count | list', array $params = array( | |
'conditions' => array('Model.field' => $thisValue), //array of conditions | |
'recursive' => 1, //int | |
//array of field names | |
'fields' => array('Model.field1', 'DISTINCT Model.field2'), // fields are foreign key (user_id, group_id) are always included in result | |
//string or array defining order | |
'order' => array('Model.created', 'Model.field3 DESC'), | |
'group' => array('Model.field'), //fields to GROUP BY | |
'limit' => n, //int | |
'page' => n, //int | |
'offset' => n, //int | |
'contain' => array(), // use false to prevent retrieve related tables | |
'callbacks' => true //other possible values are false, 'before', 'after' | |
)); | |
$type = 'count': number (0, 1, ...) | |
$type = 'first': null | Array ( | |
[ModelName] => Array | |
( | |
[id] => 83 | |
[field1] => value1 | |
) | |
[AssociatedModelName] => Array(...) | |
) | |
$type = 'all': array( | |
[0] => array( | |
[ModelName] => array(...) | |
) | |
) | |
$type = 'list': Array( | |
//[id] => 'displayValue', | |
[1] => 'displayValue1', | |
[4] => 'displayValue4', | |
[3] => 'displayValue3', | |
) | |
// more examples: | |
// 'fields' => array('User.username') | |
$justusernames = Array | |
( | |
//[id] => 'username', | |
) | |
// 'fields' => array('User.username', 'User.first_name') | |
$usernameMap = Array | |
( | |
//[username] => 'firstname', | |
) | |
// 'fields' => array('User.username', 'User.first_name', 'User.group') | |
$usernameGroups = Array | |
( | |
// [group] => array( | |
// [username] => first_name | |
//) | |
['User'] => Array | |
( | |
['PHPNut'] => 'Larry', | |
) | |
['Admin'] => Array | |
( | |
['_psychic_'] => 'John', | |
) | |
) | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
findAllBy<fieldName>(string $value, array $fields, array $order, int $limit, int $page, int $recursive) | |
$this->Product->findAllByOrderStatus('3'); // Product.order_status = 3 | |
$this->Recipe->findAllByType('Cookie'); // Recipe.type = 'Cookie' | |
$this->User->findAllByLastName('Anderson'); // User.last_name = 'Anderson' | |
$this->Cake->findAllById(7); // Cake.id = 7 | |
$this->User->findAllByEmailOrUsername('jhon', 'jhon'); // User.email = 'jhon' OR User.username = 'jhon'; | |
$this->User->findAllByUsernameAndPassword('jhon', '123'); // User.username = 'jhon' AND User.password = '123'; | |
$this->User->findAllByLastName('psychic', array(), array('User.user_name' => 'asc')); // User.last_name = 'psychic' ORDER BY User.user_name | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
findBy<fieldName>(string $value[, mixed $fields[, mixed $order]]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://book.cakephp.org/2.0/en/models/saving-your-data.html | |
$this->Model->save(Array( | |
[ModelName] => Array( | |
[fieldname1] => 'value' | |
[fieldname2] => 'value' | |
) | |
)); | |
$this->Recipe->create(); // use the function before save() when inserting new record | |
// $this: controller | |
$this->Recipe->save($this->request->data); // use the function after save() to debug: debug($this->Recipe->validationErrors); | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
Model::saveMany(array $data = null, array $options = array()) | |
// $data below are same | |
$data = array( | |
// This creates a new row | |
array('title' => 'title 1', 'Assoc' => array('field' => 'value')), | |
array('title' => 'title 2'), | |
// This updates an existing row, use id | |
array('id' => 3, 'title' => 'title 3') | |
); | |
$data = array( | |
// This creates a new row | |
array( | |
'Article' => array('title' => 'title 1'), | |
'Assoc' => array('field' => 'value') | |
), | |
array('Article' => array('title' => 'title 2')), | |
// This updates an existing row, use id | |
'Article' => array('id' => 3, 'title' => 'title 3') | |
); | |
$Model->saveMany($data, array('deep' => true)); // To save also associated data with $options['deep'] = true | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
Model::saveAssociated(array $data = null, array $options = array()) | |
// render view for $data | |
// echo $this->Form->input('User.email'); | |
// echo $this->Form->input('Cart.0.payment_status_id'); | |
// echo $this->Form->input('Cart.1.payment_status_id'); | |
$data = array( | |
'User' => array('email' => '[email protected]'), | |
'Cart' => array( | |
// 1st cart | |
array( | |
'payment_status_id' => 2, | |
'total_cost' => 250, | |
'CartItem' => array( | |
array( | |
'cart_product_id' => 3, | |
'quantity' => 1, | |
'cost' => 100, | |
), | |
array( | |
'cart_product_id' => 5, | |
'quantity' => 1, | |
'cost' => 150, | |
) | |
) | |
), | |
array(...) // 2nd cart | |
) | |
); | |
$User->saveAssociated($data, array('deep' => true)); // $deep = true will save CartItem(s) (belong to Cart). Otherwiser, just saving User and Cart(s) (belong to User) | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
// get last insert id | |
$Model->getLastInsertID() | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
// load another model | |
App::import('Model', 'GradingScale'); | |
$GradingScale = new GradingScale(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// rendering a block or retriving data | |
echo $this->fetch('sidebar'); // sidebar maybe block or data | |
// setting data | |
$this->assign('title', $post); | |
// defining a block | |
$this->start('sidebar'); | |
?> | |
<p>testing</p> | |
<?php $this->end(); ?> | |
<?php | |
// Append into the sidebar later on. | |
$this->append('sidebar'); | |
//.... | |
$this->end(); | |
// extending view | |
$this->extend('/Common/view'); | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
// Using blocks for script and CSS files | |
// in your view file | |
$this->Html->script('carousel', array('inline' => false)); // webroot/js/carousel.js | |
$this->Html->css('carousel', array('inline' => false)); // webroot/css/carousel.css | |
$this->Html->script('validation', array('block' => 'scriptBottom')); // put webroot/js/validation.js into scriptBottom block | |
$this->Html->script('datetime.js?v=123', array('block' => 'scriptBottom')); // put webroot/js/datetime.js?v=123 (must have .js if want to put parameter) | |
?> | |
// In your layout file. | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title><?php echo $this->fetch('title'); ?></title> | |
<?php echo $this->fetch('script'); ?> | |
<?php echo $this->fetch('css'); ?> | |
</head> | |
// rest of the layout follows | |
<?php echo $this->fetch('scriptBottom'); ?> | |
<?php | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
// Use another layout | |
// from a controller | |
public function admin_view() { | |
// stuff | |
$this->layout = 'admin'; // app/View/Layouts/admin.ctp | |
} | |
// from a view file | |
$this->layout = 'loggedin'; | |
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | |
// render element from /app/View/Elements/helpbox.ctp, must use echo to output element | |
echo $this->element('helpbox', array( | |
"helptext" => "Oh, this text is very helpful." | |
)); | |
// inside app/View/Elements/helpbox.ctp | |
echo $helptext; //outputs "Oh, this text is very helpful." | |
// get UesrId from view | |
$this->Session->read('Auth.User.id') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment