2012-06-17 23 views

回答

9

調查範圍。默認範圍將是您的朋友: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes

因爲defaultScopes陣列是一個函數裏面,你也可以做有條件默認範圍:

public function defaultScope() 
{ 
    $t=$this->getTableAlias(false,false); 

    if(Yii::app()->user->notAdmin()) { 
     return array(
      'condition'=>"$t.<column_name> = :<columnName>", 
      'params'=>array(':<columnName>'=>Yii::app()->user->notAdmin), 
     ); 
    } 
    else return array(); 
} 

編輯:注意,這可以給你帶來麻煩如果你不小心,請在路上行駛。請參閱this issue on the Yii site for more info

+0

是的,但是這並沒有完全解決問題。這讓我更接近了,這就是爲什麼我當時投票回來的原因。 – Turgs

2

Yii沒有辦法爲你做這件事,你可以自己做,但它很簡單。

您可以考慮範圍或查看關係並將它們全部基於當前用戶。例如,要獲得用戶的所有帖子,你可以這樣做:

$posts = Post::model()->findAll(); //WRONG 

$posts = Yii::app()->user->posts(); //RIGHT (Should define the relation in the User model) 
相關問題