2011-05-12 14 views
0

젠드 프로젝트에서 모델을 테스트하고 있는데 배열의 값을 얻는 방법에 대한 질문이 있습니다. $array[index]을 사용하여 찾을 수 없습니다.배열의 값을 얻는 방법

static function find($name, $order=null, $limit=null, $offset=null) { 
     return self::_selectAndBind(
       get_class(), 
         self::getDefaultAdapter() 
         ->select() 
         ->from('user') 
         ->where('name = ?', array($name)) 
         ->order($order) 
         ->limit($limit, $offset) 
     ); 
    } 

이 발견에 대한 테스트 케이스()이다 :

내가 테스트입니다 찾기 방법

public function testUser2CanFind() { 
     $this->assertNotNull($this->_model->find('yes')); 

     $this->assertEquals(1, count($this->_model->find('yes'))); 

     print_r($this->_model->find('yes')); 
     //$this->assertEquals('admin',$this->_model->find('yes')[0]->login); 
    } 

내가 로그인 이름의 값을 얻으려면, 그래서 우리 나는 그것이 제공 print_r($this->_model->find('yes')); :

......Array 
(
    [0] => Application_Model_User2 Object 
     (
      [_table:protected] => user 
      [_primary:protected] => Array 
       (
        [0] => id 
       ) 

      [_primary_ai:protected] => id 
      [_data:protected] => Array 
       (
        [id] => 1 
        [created] => 2011-05-03 09:41:2 
        [login] => admin 
        [password_hash] => c8ebe700df11 
        [name] => yes 
        [surname] => 
        [gender] => 
        [street] => 
        [postal_code] => 
        [city] => 
        [mobile] => 
        [homephone] => 
        [email] => 
        [is_active] => 1 
       ) 

      [_data_changed:protected] => Array 
       (
       ) 

      [_readonly:protected] => Array 
       (
        [0] => id 
       ) 

      [_db:protected] => 
     ) 

) 

가 어떻게 [login] => admin의 값을 얻을 수 ? $this->_model->find('yes')[0]을 사용하려고했지만 오류가 발생했습니다. 아무도 도와 줄 수 있습니까?

+0

Deferenced 배열이 아직 사용할 수없는 보통의 반복을 사용하여, 당신은 사용할 수 없습니다 직접 $ this- > _model-> find ('yes') [0]입니다. 임시 변수를 사용해야합니다. – grunk

+0

'$ this -> _ model-> find ('yes') -> current()'는 어떨까요? find 메서드가 행 집합을 반환한다고 가정합니다. – Marcin

+0

@Marcin : 당신이 그것을 피할 수있는 한, 당신은 "마술 방법"을 직접 부르면 안됩니다. ':: current()'는'current()'함수에 의해 호출됩니다. 대신 사용하십시오. – KingCrunch

답변

1
$entity = current($this->_model->find('yes')); 
echo $entity->login; 

업데이트 :

이 목록에 두 개 이상의 요소가있는 경우,

foreach($this->_model->find('yes') as $entity) 
    echo $entity->login; 
+0

이 배열에는 하나의 요소 만 존재하지만이 배열에 여러 요소가 있다면 어떨까요? –

관련 문제