2014-03-30 2 views
1

하나의 사용자 유형을 제외하고 사용자 테이블에서 모든 레코드를 찾고 싶습니다. 즉 사용자 테이블 dec_user이 있습니다. 여기에 속성은 user_type입니다. user_type9을 제외한 모든 레코드를 찾고 싶습니다. 그런 다음 행 수를 계산합니다. 그래서, 다음과 같이 썼습니다 :Yii에 findAll 레코드를 계산하십시오.

$user_type = 9; 
    return count(User::model()->findAll(array("condition"=>"':user_type' != $user_type"))); 

실제로이 조건을 쓰는 방법을 모르겠습니다.

답변

6

데이터베이스에서 배열을 검색 할 필요없이 PHP count() 함수를 사용하여 계산할 수 있습니다.

YII 방법 :

return User::model()->count('user_type <> '.$user_type); 

또는 사용 PARAMS의 :

return User::model()->count('user_type <> :type', array('type' => $user_type); 

또는, 당신은 SQL 쿼리를 구축하려는 경우, CommandBuilder가 사용

return Yii::app()->db->createCommand() 
      ->select('COUNT(*)') 
      ->from('user') 
      ->where('user_type <> '.$user_type) 
      ->queryScalar(); 
+0

최고, 감사합니다 – StreetCoder