不能使用类型为相册模型相册的对象作为数组 - ZF2.3


Cannot use object of type AlbumModelAlbum as array - ZF2.3

我正在尝试按照Zend官方网站上的专辑列表教程进行操作,但是我遇到了一个错误,我找不到解决方案。我正在从网站上复制代码,但它显示了我在标题中写的错误。

该类中的代码位于文件Album.php

 public function exchangeArray($data)
 {
     $this->id = (isset($data['id'])) ? $data['id'] : null;
     $this->artist = (isset($data['artist'])) ? $data['artist'] : null;
     $this->title = (isset($data['title'])) ? $data['title'] : null;
 }

当我尝试编辑我拥有的相册并访问文件时,就会发生这种情况edit.phtml

 <?php
 $title = 'Edit album';
 $this->headTitle($title);
 ?>
 <h1><?php echo $this->escapeHtml($title); ?></h1>
 <?php
 $form = $this->form;
 $form->setAttribute('action', $this->url(
     'album',
      array(
         'action' => 'edit',
         'id'     => $this->id,
      )
 ));
 $form->prepare();
 echo $this->form()->openTag($form);
 echo $this->formHidden($form->get('id'));
 echo $this->formRow($form->get('title'));
 echo $this->formRow($form->get('artist'));
 echo $this->formSubmit($form->get('submit'));
 echo $this->form()->closeTag();
 ?>

我不知道错误可能在哪里。我是Zend的新手。如果您能检查我到目前为止在我的 github 中所做的工作,我将不胜感激。

完整的错误消息是:

Fatal error: Cannot use object of type Album'Model'Album as array in /var/www/html/zf/module/Album/src/Album/Model/Album.php on line 17

谢谢。

问题解决了。碰巧我已经复制了addAction()方法,我不得不删除两行才能使editAction()正常工作。

您可能需要将对象绑定到窗体: $form->bind($album);