【问题标题】:ZF2 TableGateway doesn't works when insert a row插入行时 ZF2 TableGateway 不起作用
【发布时间】:2013-01-29 10:30:40
【问题描述】:

当我打算在 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>

我得到这个错误:

可捕获的致命错误:无法将 stdClass 类的对象转换为 D:\xampp\htdocs\haystack\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Statement.php 中的字符串258

我知道错误的类型('stdClass 无法转换为字符串'),但我不明白发生了什么......

感谢任何帮助

亲切的问候。

【问题讨论】:

    标签: zend-framework2 tablegateway


    【解决方案1】:

    希望这会对您有所帮助。这是我的 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
    ));
    

    【讨论】:

    • 您好,非常感谢。我发现了错误,最后一刻在实体模型中添加了一个变量,在它的 get 方法中没有控制..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2023-03-25
    相关资源
    最近更新 更多