当我想在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>
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:
我收到了这个错误:
捕获致命错误:无法将类stdClass的对象转换为D:\ xampp \ htdocs \ haystack \ vendor \ zendframework \ zendframework \ library \ Zend \ Db \ Adapter \ Driver \ Pdo \ Statement.php中的字符串258
我知道错误的类型('stdClass无法转换为字符串'),但我不明白发生了什么......
感谢任何帮助
亲切的问候。
答案 0 :(得分:0)
希望这会帮助你。这是我的TableGateway
class Domains extends AbstractTableGateway
{
public function __construct($adapter)
{
$this->table = 'domains';
$this->adapter = $adapter;
$this->initialize();
}
}
以下是我插入数据的方式:
$this->getTableDomains()->insert(array(
'companyid' => $params['companyid'],
'domain_id' => $result->id,
'name' => $name,
'type' => strtoupper($params['type']),
'content' => strtolower($params['content']),
'ttl' => $ttl,
'prio' => $prio
));