SQLDUMP
/* controller */
print_r($this->MyModel->getDataSource()->getLog());
/* view */
<?php //echo $this->element('sql_dump'); ?>
Modelのテーブル指定
<?php
App::uses('Model', 'Model');
class MyModel extends AppModel {
public $useTable = 'MyTable';
}
Model Validation
App::uses('Model', 'Model');
class User extends AppModel {
public $validate = array(
'user_name' => array(
'notEmpty' => array(
'rule' => 'notBlank',
'required' => true,
'allowEmpty' => false,
'message' => '1文字以上入力してください',
),
'maxLength' => array(
'required' => true,
'allowEmpty' => false,
'rule' => array('maxLength', 255),
'message' => '255文字以内で入力してください',
),
),
'postal_code' => array(
'rule' => array('custom', '/^[0-9]{3}-[0-9]{4}$/'),
'message' => 'ハイフン付き郵便番号の形式で入力してください',
'allowEmpty' => true,
),
'address' => array(
'notEmpty' => array(
'rule' => 'notBlank',
'required' => true,
'allowEmpty' => false,
'message' => '1文字以上入力してください',
),
),
'tel' => array(
'rule' => array('custom', '/^[0-9]-$/'),
'message' => 'ハイフン付き郵便番号の形式で入力してください',
'allowEmpty' => false,
),
'url' => array(
'rule' => array('url'),
'message' => 'URLの形式で入力してください',
'allowEmpty' => true,
),
);
}
/* 使うとき */
if ($this->User->validates()) {
... // validation 成功
}
// viewに渡す
$this->set('valerror', $this->[Model名]->validationErrors);
// セッションに渡す
$this->Session->setFlash( $this->[Model名]->validationErrors);
paginator
$this->paginate = array (
'User' => array (
'conditions' => $conditions,
'limit' => 50,
'order' => 'User.id DESC',
'fields' => '*',
'joins' => array (
array (
'type' => 'INNER',
'table' => 'group_members',
'alias' => 'GroupMember',
'conditions' => 'GroupMember.user_id = User.id'
),
array (
'type' => 'INNER',
'table' => 'groups',
'alias' => 'Group',
'conditions' => 'GroupMember.group_id = Group.id'
)
)
),
);
$users = $this->Paginator->paginate('User');
コントローラーでのJoin
$this->User->recursive = -1;
$users = $this->User->find('all', array(
'fields' => 'User.*',
'conditions' => array(
'User.status' => 1,
),
'joins' => array (
array (
'type' => 'INNER',
'table' => 'group_members',
'alias' => 'GroupMember',
'conditions' => 'User.id = GroupMember.user_id',
),
array (
'type' => 'INNER',
'table' => 'groups',
'alias' => 'Group',
'conditions' => 'Group.id = GroupMember.group_id',
),
),
));
Form生成url指定
<?php echo $this->Form->create(null, array(
'url' => '/users/register',
'type' => 'post',
'inputDefaults' => array(
'label' => false,
'div' => false
),
'class' => '',
'id' => 'UserRegisterForm',
)); ?>
...
<?php echo $this->Form->end(); ?>
Flash
/* コントローラー内 */
$errorMessage = 'エラーだよ';
$this->Flash->set($errorMessage, array('key' => 'errorMessage'));
return $this->redirect(array('action' => 'login'));
/* view */
<?php echo $this->Flash->render('errorMessage'); ?>
Form項目生成いろいろ
/* select */
<?php echo $this->Form->input('status',
array('type' => 'select',
'label' => false,
'legend' => false,
'div' => false,
'error' => false,
'options' => array('0' => '無効', '1' => '有効'),
'default' => 1
)); ?>
/* check */
<?php echo $this->Form->input('checkbox1', array(
'type' => 'checkbox',
'class' => '',
'id' => 'checkbox1',
'hiddenField' => false,
'value' => 'チェックボックス1',
'error' => false
)); ?>
/* text */
echo $this->Form->input('url', array(
'label' => false,
'div' => false,
'type' => 'text',
'required' => true,
'size' => 60
)); ?>
/* hidden */
<?php echo $this->Form->hidden('user_id', array('value' => 1)); ?>
/* date */
<?php echo $this->Form->input('st_date', array(
'div' => false,
'label' => false,
'type' => 'date',
'dateFormat' => 'YMD',
'timeFormat' => '24',
'monthNames' => false,
'empty' => false,
'minYear' => 2018,
'maxYear' => date('Y', strtotime(date('Y-m-1').' +1 year')),
'after' => '~',
'default' => $start_date_timestamp,
)) ?>
<?php echo $this->Form->input('en_date', array(
'div' => false,
'label' => false,
'type' => 'date',
'dateFormat' => 'YMD',
'timeFormat' => '24',
'monthNames' => false,
'empty' => false,
'minYear' => 2019,
'maxYear' => date('Y', strtotime(date('Y-m-1').' +1 year')),
'default' => $end_date_timestamp,
)) ?>
/* image */
<?php echo $this->Html->image($imageUrl, array('class'=>'', 'alt' => ''));?>
/* textarea */
<?php echo $this->Form->input('remarks', array('type' => 'textarea', 'class' => '', 'id' => 'remarks', 'placeholder' => 'プレイスホルダー', 'required' => 'required')); ?>
ブラウザキャッシュ対策
/* core.php とか */
Configure::write('Asset.timestamp', 'force');