2013-10-07 11 views
0

내가 데이터베이스에서 모델을 검색 한 후이YII PHP : 연관 배열의 특정 키를 가진 값을 얻기

id | group_id| first_name | middle_name | last_name 
------------------------------------------------------ 
    |   |   |    |  
------------------------------------------------------ 

처럼 보이는 모델/데이터베이스 테이블이 있습니다

말 :

$people = PersonModel::model()->findAllByAttributes(array('group_id'=>$groupId)); 

하고 있습니다 ..의 groupId는

내가 원하는

을 부여 매칭 내가 10 개 행을 검색 한 가정 일치하는 10 개의 행 중 first_name을 모두 저장합니다. 예를 들어,

$personArray = array(); 
foreach($people as $person){ 
    $personArray[] = $person->first_name; 
} 

하지만 다른 방법이 :이은 수행 할 수있어

같은 일을하는 PHP 함수? 고맙습니다!

답변

3
$criteria = new CDbCriteria; 
$criteria->compare('group_id', $groupId); 
$criteria->limit = 10; 
$people = PersonModel::model()->findAll($criteria); 
$personArray = $list = CHtml::listData($people, 'id','first_name'); 

등 함께 연관 배열을 반환CHTML ::되는 listData() : 'ID'] => '의 first_name "(모델의 특성)

0

처음처럼 GROUP_ID, 일치하는 모델을 셀 수 :

PersonModel :: 모델() -> countByAttributes (배열 ('GROUP_ID'=> $의 groupId));

10 개로 제한하고 desc를 주문하고 일치하는 최신 10 개의 행을 얻을 수 있습니다!

Yii : how to count records in a model?

관련 문제