CakePHPを用いて複数のチェックボックスの値をサーバサイドで取得する:東京ウェブ制作ブロマガ:東京ウェブ制作チャンネル(大和賢一郎) – ニコニコチャンネル:生活

‘value’ => array(‘0’, 3)

引用元: CakePHPを用いて複数のチェックボックスの値をサーバサイドで取得する:東京ウェブ制作ブロマガ:東京ウェブ制作チャンネル(大和賢一郎) – ニコニコチャンネル:生活.

cakephp2.4 [Mac] bake

bakeがなんだか出来なかった訳
unix_socketってのを追加した
[php]
public $default = array(
‘datasource’ => ‘Database/Mysql’,
‘persistent’ => false,
‘host’ => ‘localhost’,
‘unix_socket’ => ‘/Applications/MAMP/tmp/mysql/mysql.sock’,
‘login’ => ‘root’,
‘password’ => ‘root’,
‘database’ => ‘cakedictionary’,
‘prefix’ => ”,
//’encoding’ => ‘utf8’,
);
[/php]

複数checkbox 1フィールドで配列で取得

[php]
//コントローラー
public function add() {
   if ($this->request->is(‘post’)) {

if($this->request->data[‘Lecture’][‘name’] == false){
       //対象データがなかった処理
array(‘Lecture’ => array(
‘name’ => ‘test’
));
}
if ($this->request->data[‘Lecture’][‘name’] == true) {
//対象データがあった処理
$this->request->data[‘Lecture’][‘name’] = implode( ‘,’, $this->data[‘Lecture’][‘name’]); //カンマで区切るよ
}
     
     $this->Lecture->create();
   if ($this->Lecture->save($this->request->data)) {
$this->Session->setFlash(__(‘The lecture has been saved.’));
 return $this->redirect(array(‘action’ => ‘index’));
   } else {
$this->Session->setFlash(__(‘The lecture could not be saved. Please, try again.’));
}
    }
$numbers = $this->Lecture->Number->find(‘list’);
$this->set(compact(‘numbers’));
}
[/php]
[php]
//view
$select1 = array(‘test’,’test1′);
echo $this->Form->input( ‘name’, array(
‘type’ => ‘select’,
‘multiple’=> ‘checkbox’,
‘options’ => $select1,
//’selected’ => $selected // 規定値は、valueを配列にしたもの
));
[/php]
[php]
//editコントローラー
$test= $this->Lecture->find(‘first’, array(
‘conditions’ => array(‘Lecture.id’ => $id),
‘fields’ => array(‘Lecture.name’), //フィールド名の配列
));
$this->set( ‘selected’, explode( ‘,’, $this->$test[‘Lecture’][‘name’]));
[/php]
[php]
//view editコントローラー
$select1 = array(‘test’,’test1′);
echo $this->Form->input( ‘name’, array(
‘type’ => ‘select’,
‘multiple’=> ‘checkbox’,
‘options’ => $select1,
‘selected’ => $selected // 規定値は、valueを配列にしたもの ここね
));
[/php]

参考:http://kwski.net/cakephp-2-x/1088/

(CakePHP2.x)独自バリデーションルールの書き方 | 日々の覚書…日常のことも少しだけ

//独自バリデーションルール

public function checkOnly($check){

  //チェックしたいルールを書く

  if (!empty($this->data[‘Brunch’][‘id’])){

      //存在するか確認

      $count = $this->find(‘count’, array(‘conditions’ => array(

                               ‘brunch_name’ => $check[‘brunch_name’],

                               ‘company_id’ => $this->data[‘Brunch’][‘company_id’],

                               ‘NOT’ => array(‘Brunch.id’ => $this->data[‘Brunch’][‘id’])

      )));

      //1件でもあればfalseを返す

      return $count == 0;

  }else{

      //新規の場合

      //存在するか確認

      $count = $this->find(‘count’, array(‘conditions’ => array(

   

引用元: (CakePHP2.x)独自バリデーションルールの書き方 | 日々の覚書…日常のことも少しだけ.

バインド アソシエーション bindModel

常に連結させたくないテーブルがあり、場合によっては連結させてデータを取りたいときも出てくる
そんなときに「メソッドが呼び出されたタイミングで連結を行う」これをバインドという
[php]
public function adduser(){
$belongsTo = array(
‘User’ => array(
‘className’ => ‘User’,
‘foreignKey’ => ‘user_id’
));
$this->bindModel(array("belongsTo" => $belongsTo));
$data = $this->find(‘all’);
return $data;
}
[/php]

参考 : http://offsidenow.phpapps.jp/archives/1447

ログインしているユーザーIDで保存する

これを保存する際に追加
[php]$this->request->data[‘Post’][‘user_id’] = $this->Auth->user(‘id’); [/php]

[php]
public function add() {
if ($this->request->is(‘post’)) {
$this->Post->create();
$this->request->data[‘Post’][‘user_id’] = $this->Auth->user(‘id’); //postテーブルのuser_idにログインしているユーザー情報を保存するよ
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__(‘The post has been saved.’));
return $this->redirect(array(‘action’ => ‘index’));
} else {
$this->Session->setFlash(__(‘The post could not be saved. Please, try again.’));
}
}
$users = $this->Post->User->find(‘list’);
$this->set(compact(‘users’,’options’));

}
[/php]

参考:http://offsidenow.phpapps.jp/archives/1447

Webメモ