-
Notifications
You must be signed in to change notification settings - Fork 6
Sample codes
hiromi2424 edited this page Sep 14, 2010
·
2 revisions
Here is a simple Post Model.
<?php
class Post extends AppModel {
var $hasMany = array('Comment');
var $hasOne = array('Status');
var $acsAs = array('Collectionable.options');
var $defaultOption = true; // or string like 'default'
var $options =array(
'default' => array(
'contain' => array(
'Comment',
'Status',
),
'limit' => 10,
),
'published' => array(
'condtiions' => array('Status.published' => true),
),
'recent' => array(
'order' => ('Post.updated DESC'),
),
'rss' => array(
'limit' => 15,
),
'unlimited' => array(
'limit' => null,
),
'index' => array(
// You can do sub merging
'options' => array(
'published',
'recent',
),
),
);
}
?>
You can use them by like:
<?php
class PostsController extends AppController {
function index() {
$this->paginate = $this->Post->options('index');
$this->set('posts', $this->paginate());
}
function rss() {
$this->paginate = $this->Post->options('index', 'rss'); // multiple merging at run time;
$this->set('posts', $this->paginate());
}
function all_in_one_page() {
// you can use "options" attribute wihtin finding options
$posts = $this->Post->find('all', array('options' => array('index', 'unlimited')));
$this->set(compact('posts'));
}
}
This sample uses MatchableBehavior
<?php
class User extends AppModel {
var $hasMany = array('Post');
var $actsAs = array('Collectionable.VirtualFields', 'Matchable');
var $virtualFields = array(
'full_name' => "CONCAT(User.first_name, ' ', User.last_name)",
);
var $virtualFieldsCollection = array(
'posts_count' => 'COUNT(Post.id)',
'amount_used' => 'SUM(Post.volume)',
);
}
You can use them by like:
<?php
class UsersController extends AppController {
function admin_index() {
// Hey, you may feel like using OptionsBehavior :P
$jointo = array('Post');
$group = 'User.id';
$virtualFields = array('posts_count', 'amount_used'); // key of collections
$this->paginate = compact('jointo', 'group', 'virtualFields');
$this->set('users', $this->paginate());
}
function profile() {
$virtualFields = array('full_name' => false); // disable virtualFields
$user = $this->User->find('first', compact('virtualFields'));
$this->set(compact('user'));
}
}