Zend framework2 如何在Zend 2中执行更新查询?
我尝试不使用函数execute(),我尝试以以下方式执行更新查询:Zend framework2 如何在Zend 2中执行更新查询?,zend-framework2,Zend Framework2,我尝试不使用函数execute(),我尝试以以下方式执行更新查询: public function __construct(Adapter $adapter) { ini_set('display_errors', true); $this->adapter = $adapter; } public function getUserProfile($array_post) { $data = array( 'username' => $arr
public function __construct(Adapter $adapter) {
ini_set('display_errors', true);
$this->adapter = $adapter;
}
public function getUserProfile($array_post) {
$data = array(
'username' => $array_post['username'],
'email' => $array_post['mail']
);
$where = $this->adapter->quoteInto('id = ?', $array_post['userid']);
$this->update($data, $where);
}
$data = array(
'username' => $array_post['username'],
'email' => $array_post['mail']
);
$update = $this->adapter->update();
$update = $update->set($data);
$update->where(array('id' => $array_post['userid']));
但是我不知道ZEND 2中这个quoteInto()的等价函数是什么。现在我得到一个空白页,当它被执行时,没有错误,什么也没有。你能帮我用这个方法吗?thx
更新我也尝试了以下方法:
public function __construct(Adapter $adapter) {
ini_set('display_errors', true);
$this->adapter = $adapter;
}
public function getUserProfile($array_post) {
$data = array(
'username' => $array_post['username'],
'email' => $array_post['mail']
);
$where = $this->adapter->quoteInto('id = ?', $array_post['userid']);
$this->update($data, $where);
}
$data = array(
'username' => $array_post['username'],
'email' => $array_post['mail']
);
$update = $this->adapter->update();
$update = $update->set($data);
$update->where(array('id' => $array_post['userid']));
与Zend 1相比,Zend 2中的数据库工作方式有很大不同。适配器是一个非常轻量级的对象,不包含任何CRUD方法。如果要使用这些方法,则需要使用TableGateway 或者,您可以创建一个SQL对象,然后使用适配器执行它
我创建了一个非常简单的活动记录实现(非常类似于Zend 1表),使它变得更简单。如果您想查看它,这里有一个链接:这是一个使用ForrestLyman提到的
TableGateway
的示例
public function updateAction($data)
{
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$update = $sql->update();
$update->table('table_name');
$update->set($data);
$update->where('action_id = '.$data['action_id'].'');
$statement = $sql->prepareStatementForSqlObject($update);
$result = $statement->execute();
$flag = $result->getAffectedRows();
if(!($flag)){
return false;
}else{
return true;
}
}
第二个示例中的适配器是什么?您可以通过
get\u类($adapter)获得此功能代码>适配器是在构造函数中调用的。或者,我正在查找完整的限定名,以确保您没有使用某些自定义适配器。希望我下面的回答能有所帮助。