//こちらは検索結果をすべて取得する例
$timetables = $this->Timetable->find('all',array(
'conditions' => array('Timetable.userid' => $vid ,'DATE(Timetable.startdate) =' => $ddate ),
'fields' => array('Timetable.startdate', 'DISTINCT Timetable.startdate'),
'order' => array('Timetable.startdate' => 'ASC'),
'group' => array('Timetable.startdate'),
'limit' => 50
));


このallの場所をfirstにすれば、最初の1件のみ取得することが出来る。

もし別のテーブルを取得する場合には次の宣言が必要
//使いたいコントローラーの上にて宣言する。
var $uses = array('User','Prefecture','Category');
注意点は必ず追加したい、テーブル名と自分自身のテーブル名も含める。

例えば今回の例で言うとコントローラーが
users_controller.phpでその中でPrefectureテーブルとCategoryテーブルを
使用したい場合になります。もちろんテーブル名はクラス名なので、実際の
テーブル名は複数系になります。


<script type="text/javascript"><!--
google_ad_client = "ca-pub-7245466150014293";
/* ameblog */
google_ad_slot = "8734624761";
google_ad_width = 728;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js ">
</script>

今回のテーマはグループごとに処理を分ける方法です。
意外と必要な要件な気がしますが、検索してもなかなかよい
サンプルを見つけることが出来ず、約2日間も解決に費やしました。(/_;)


ログイン後に表示させるトップページをグループごとに変更するのですが
どのタイミングで変更すればいいのか分からずとても苦労しました。
ログインの認証っていったい全体どこでやっているのか、そこをつきとめるのが
なかなか出来なかったです。
下記に自分なりに分かった事を記述します。


まずログイン画面でログインボタンを押してからの処理です。


1.app_Controller.phpが呼ばれる
2.user_controller.phpのbeforeFilter処理が呼ばれる
3.この後にリダイレクト先($this->Auth->loginRedirect)が呼ばれる


こんな感じなので、2で処理を行い、変更を試みたのですが反映されず
結局下記の流れになりました。


1.app_Controller.phpが呼ばれる・・・$this->Auth->loginRedirectを無効に設定
2.user_controller.phpのbeforeFilter処理が呼ばれる
3.user_controller.phpのlogin処理が呼ばれる
4.login処理の中で、_login処理を呼ぶ
5._login処理の中でグループを取得して、処理分岐を行っています。


はまったところは、やるたびに違う動きをしていて、テストをブラウザの戻るボタンを
押してやっていたので、ログアウト処理がうまく出来てなくて、たまにうまくいっていました。


こちらがサンプルになります。


[app_controller.php]
<?php
class AppController extends Controller {
var $components = array('Acl', 'Auth', 'Session');
function beforeFilter() {
// AuthComponent の設定
$this->Auth->authorize = 'actions';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
//$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'csensei');
//$this->Auth->logoutRedirect = $this->redirect('http://www.aaaaa.com');
$this->Auth->loginError = 'ログインエラーです';
$this->Auth->authError = '権限がありません';

$this->Auth->fields = array('username' => 'email','password' => 'password');

$this->set('user',$this->Auth->user());
}
}
?>


[users_controller.php]
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('*');
$userdata = $this->Auth->user();
}

function login() {
if ($this->Auth->user()) {

}else{
$this->Session->setFlash(__('パスワードが違います。', true));
}
$this->_login();
}

Function _login(){
if ($this->Auth->user()) {
if($this->Auth->user('group_id') == 1){
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
}elseif($this->Auth->user('group_id') == 2){
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'csensei');
}elseif($this->Auth->user('group_id') == 3){
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'cseito');
}
$userdata = $this->Auth->user();
$this->redirect($this->Auth->loginRedirect);
}
}