$this->set( ‘select1’, $this->Prefecture->find( ‘list’, array(
‘fields’ => array( ‘id’, ‘prefecture’)
$this->set( ‘select1’, $this->Prefecture->find( ‘list’, array(
‘fields’ => array( ‘id’, ‘prefecture’)
仮に記事とタグのテーブルを作り、CakePHPでそのモデルを作ったら、こんな感じになる。ここでは記事テーブルをposts、
常に連結させたくないテーブルがあり、場合によっては連結させてデータを取りたいときも出てくる
そんなときに「メソッドが呼び出されたタイミングで連結を行う」これをバインドという
[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]
これを保存する際に追加
[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]
public function beforeFilter(){
$this->set(‘userSession’, $this->Auth->user());
}
debugモードが1以上の場合はリダイレクトしないのかよ!?
app/config/core.php
[php]
Configure::write(‘debug’, 0);
[/php]
acl_add