当我打算在 DB 中插入一行时,我遇到了一个致命错误。我不明白发生了什么,我阅读了一些博客但没有解决方案,我的代码与 Evan 在他的博客中发布的示例相同。
我的模特班
class CommentTable
{
protected $_commentTableGateway;
protected $_hydratator;
protected $_resultSet;
public function __construct($adapter)
{
$this->_hydratator = new \Zend\Stdlib\Hydrator\ClassMethods;
$rowObjectPrototype = new Comment();
$this->_resultSet = new \Zend\Db\ResultSet\HydratingResultSet($this->_hydratator, $rowObjectPrototype);
$this->_commentTableGateway = new TableGateway('comments', $adapter, null, $this->_resultSet );
}
public function fetchAll()
{
return $this->_commentTableGateway->select();
}
public function saveComment(Comment $comment)
{
$id = (int)$comment->getId();
if ($id == 0) {
$this->_commentTableGateway->insert($this->_hydratator->extract($comment));//this fails
} else {
if ($this->getComment($id)) {
$this->_commentTableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('El comentario que queire editar no exite');
}
}
}
public function getComment($id)
{
$id = (int) $id;
$rowset = $this->_commentTableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
}
</code>
<code>
In module class:
//a factory in service manager
'Comment\Model\CommentTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new CommentTable($dbAdapter);
return $table;
},
</code>
<code>
My controller:
public function getCommentTable()
{
if (!$this->_commentTable) {
$sm = $this->getServiceLocator();
$this->_commentTable = $sm->get('Comment\Model\CommentTable');
}
return $this->_commentTable;
}
</code>
我得到这个错误:
可捕获的致命错误:第 258 行的 D:\xampp\htdocs\haystack\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Statement.php 中的类 stdClass 的对象无法转换为字符串
我知道错误的类型('stdClass 无法转换为字符串'),但我不明白发生了什么......
任何帮助表示赞赏
亲切的问候。