我在zend项目中测试模型,我有一个关于如何获取数组值的问题,我发现使用$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)
);
}
这是find()的测试用例:
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 :(得分:1)
$entity = current($this->_model->find('yes'));
echo $entity->login;
更新
如果此列表中有多个元素,请使用常规迭代
foreach($this->_model->find('yes') as $entity)
echo $entity->login;