Php ZF2绑定表单数据

Php ZF2绑定表单数据,php,forms,zend-framework,bind,Php,Forms,Zend Framework,Bind,我试图在设置编辑的表单数据之前绑定表单数据,因为我不想丢失未更改的值。它实际上给我带来了一个错误,ZF2网站并没有为我提供一个很好的工作示例。我被卡住了,我不想写一个肮脏的变通方法,有人吗?:) 我创建了一个模型,如下所示: namespace Application\Model; class Advertisement { public $id; public $name; public $code; public $banner; public $fileupload; public

我试图在设置编辑的表单数据之前绑定表单数据,因为我不想丢失未更改的值。它实际上给我带来了一个错误,ZF2网站并没有为我提供一个很好的工作示例。我被卡住了,我不想写一个肮脏的变通方法,有人吗?:)

我创建了一个模型,如下所示:

namespace Application\Model;

class Advertisement {

public $id;
public $name;
public $code;
public $banner;
public $fileupload;


public function exchangeArray($data)
{
    $this->id                = isset($data['id'])               ? $data['id']                    : null;
    $this->name              = isset($data['name'])             ? $data['name']                  : null;
    $this->code              = isset($data['code'])             ? $data['code']                  : null;
    $this->banner            = isset($data['banner'])           ? $data['banner']                : '';
    $this->fileupload        = isset($data['fileupload'])       ? $data['fileupload']            : '';
}

public function exchangeJsonToPhpArray($json)
{

}

public function getArrayCopy()
{
    return get_object_vars($this);
}
}

我的控制器

public function editAction()
     {

        # Get Request and Params
        $request  = $this->getRequest();
        $language = $this->params('language');
        $id       = $this->params('id');

        # Get advertisement data
        $oAdvertisement = new Advertisement();
        if (!$oAdvertisement = $this->getAdvertisementTable()->selectAdvertisementToEdit(array('id' => $id)))
            return $this->redirect()->toRoute('admin/advertisement');

        # Advertisement form
        $sm        = $this->getServiceLocator();
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $form      = new AdvertisementForm($dbAdapter, $this->params('language'));
        $form->bind($oAdvertisement);

        # Post request
        $request = $this->getRequest();
        if ($request->isPost()) {

            # Set post data
            $post = array_merge_recursive($this->request->getPost()->toArray(), $this->request->getFiles()->toArray());
            $form->setData($post);

            # Validate form
            if ($form->isValid()) {

              # Get form data
              $formData = $form->getData();

              Debug::dump($form->getData());

              # Get service config
              $serviceLocator  = $this->getServiceLocator();
              $config          = $serviceLocator->get('config');
              $sBannerName     = $config['banner_upload_path'] . '/' . md5(mt_rand()) .'.jpg';

              # Insert into database
              $oAdvertisement->exchangeArray($formData);

              # Validate and rename image upload
              //.. image upload

              Debug::dump($oAdvertisement);
              exit;

              # Update database record
              $this->getAdvertisementTable()->updateAdvertisement($oAdvertisement, $id);
           // success..

            } else {

               // false..
            }
        }

        # Return view model
        return new ViewModel(array(
            'form' => $form,
        ));
    }
错误

Fatal error: Cannot use object of type Application\Model\Advertisement as array in ..../module/Application/src/Application/Model/Advertisement.php on line 29
在exchangeArray()方法中,尝试测试参数$data的类型。 就我个人而言,我系统地提出:

if ($data instanceof IteratorAggregate) {
    $data = iterator_to_array($data->getIterator(), true);
} elseif ($data instanceof Iterator) {
    $data = iterator_to_array($data, true);
} elseif (! is_array($data)) {
    throw new Exception(_('Data provided is not an array, nor does it implement Iterator or IteratorAggregate'));
}
在exchangeArray()方法中,尝试测试参数$data的类型。 就我个人而言,我系统地提出:

if ($data instanceof IteratorAggregate) {
    $data = iterator_to_array($data->getIterator(), true);
} elseif ($data instanceof Iterator) {
    $data = iterator_to_array($data, true);
} elseif (! is_array($data)) {
    throw new Exception(_('Data provided is not an array, nor does it implement Iterator or IteratorAggregate'));
}