CakePHP very small tips

Before working with “Prefix” must :

– Config in core (app\Config\core.php)  : Configure::write(‘Routing.prefixes’, array(‘admin’, ‘customer’));

– Config in route (app\Config\routes.php):

Router::connect(‘/admin’, array(‘controller’ => ‘dashboard’, ‘action’ => ‘index’, ‘admin’ => true));
Router::connect(‘/admin/login’, array(‘controller’ => ‘login’, ‘action’ => ‘login’, ‘admin’ => true));
Router::connect(‘/admin/logout’, array(‘controller’ => ‘login’, ‘action’ => ‘logout’, ‘admin’ => true));
Router::connect(‘/login’, array(‘controller’ => ‘login’, ‘action’ => ‘login’));
Router::connect(‘/logout’, array(‘controller’ => ‘login’, ‘action’ => ‘logout’));
Router::connect(‘/customer/login’, array(‘controller’ => ‘login’, ‘action’ => ‘login’, ‘customer’ => true));
Router::connect(‘/customer’, array(‘controller’ => ‘users’, ‘action’ => ‘view’, ‘customer’ => true));

About prefix authenticate. Include some code to AppController (to determain Model + Table  for Authen Login/Logout):

if (isset($this->request->params[‘prefix’]) && $this->request->params[‘prefix’] == ‘customer’) {
$this->Auth->authenticate = array(
‘Form’ => array(
‘userModel’ => ‘Customers.Customer’,
‘scope’ => array(‘Customer.del_flg’ => 0),
)
);
};

Sample for Paginate with custom fields (not from Model)

$priority = ‘(CASE ‘;
$count = 0;
foreach ($shopList as $item) {
$priority .= ‘ WHEN Order.shop_id = ‘.$item.’ THEN ‘.$count .’ ‘;
$count += 1;
}
$priority .= ‘END) as rank’;

$this->paginate[‘Order’] = array(
‘limit’=> 10,
‘recursive’=>-1,
‘conditions’ => array(
‘Order.shop_id’ => $shopList,
‘Order.user_id’ => $this->Auth->user(‘id’),
‘Order.status’ => array(ORDER_STATUS_PAID,ORDER_STATUS_POINT),
‘Order.accounting_date <>’ => null,
‘Order.del_flg’ => 0),
‘joins’ => array(
array(
‘table’ => ‘tb_shops’,
‘alias’ => ‘Shop’,
‘type’ => ‘inner’,
‘conditions’ => array(‘Shop.id = Order.shop_id AND Shop.del_flg = 0’)
)),
‘fields’=> array(‘Order.id’,’Order.shop_id’,’Order.accounting_date’,$priority, ‘Shop.mobile_booking_flg’),
‘order’ => array(‘rank’, ‘Order.accounting_date DESC’),
);
$orders = $this->paginate(‘Order’);