2015-06-10 65 views
1

我有工作職能:CakePHP的MySQL查詢與WHERE

public function index() 
    { 
     $this->set('tables', $this->Table->find('all')); 
    } 

但如何只顯示的列,其中所有者=登錄的用戶?

+0

你怎麼知道,如果用戶登錄?你如何驗證用戶? – Michel

+0

公共$組分=陣列( '會話', '驗證'=>數組( 'loginRedirect'=>數組( '控制器'=> '表', '動作'=> '索引' ), 'logoutRedirect'=>數組( '控制器'=> '表', '動作'=> '索引' ) '認證'=>數組( '窗體'=>數組( 'passwordHasher' =>'Blowfish' ) ) ) );如果(isset($ user ['role'])&& $ user ['role'] ==='admin'){ return true;} { – andrzej

+0

public function isAuthorized($ user){0} { //管理員可以訪問每個動作 。 } //默認拒絕 return false; } – andrzej

回答

1

你想要做這樣的事,其中$this->Session->read()返回當前認證的用戶: -

$this->Table->find(
    'all', 
    array(
     'conditions' => array('Table.user_id' => $this->Session->read("Auth.User.id")) 
    ) 
); 
+0

解析錯誤:語法錯誤,意外T_DOUBLE_ARROW – andrzej

+0

對不起,錯字。修復了答案。忘記在'conditions'索引後加入'array'。 – drmonkeyninja

+0

foreach中的錯誤 <? foreach($ tables as $ table):?> – andrzej