Zend_Form :: populate()错误

时间:2010-07-29 08:06:16

标签: php zend-framework

我在这里遇到了一些问题。当我尝试这个函数时,它会返回这个错误:

Catchable fatal error: Argument 1 passed to Zend_Form::populate() must be an array, object given, called in [..]

当我使用print_r()查找第一个参数的值时,输出为:

Zend_Db_Table_Row Object ( [_data:protected] => Array ( [id] => 4 [title] => sd [name] => KG [picture] => http://xx/images/mny4r64mqb.png [show] => 1 [body] =>KB
) [..]

所以我知道,我输入的对象是一个数组。什么可能导致这个问题? 模型

public function getUser($id) 
    {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        $row->toArray();
        if (!$row) {
            throw new Exception("Could not find row $id");
        }
        return $row;    
    }

控制器:

 $albums = new Admin_Model_Users ();
                    //print_r($albums->getUser($id));
                    $form->populate ( $albums->getUser ( $id ) );

1 个答案:

答案 0 :(得分:1)

您必须使用 toArray() Zend_Db_Table_Row 转换为数组,然后传递给 populate()

Zend_Db_Table_Row

示例:

$bugs = new Bugs();
$row = $bugs->fetchRow($bugs->select()->where('bug_id = ?', 1));

// Get the column/value associative array from the Row object
$rowArray = $row->toArray();

// Now use it as a normal array
foreach ($rowArray as $column => $value) {
    echo "Column: $column\n";
    echo "Value:  $value\n";