Php 截断表+;Zend框架
我这样做:Php 截断表+;Zend框架,php,sql,database,zend-framework,Php,Sql,Database,Zend Framework,我这样做: $data = array('coords' => $district->getCoords(), 'id' => $district->getId(), 'fid' => $district->getFid(), 'wijziging'=> $district->getWijziging(), 'nieuwnr' => $district->getNieuwnr(
$data = array('coords' => $district->getCoords(),
'id' => $district->getId(),
'fid' => $district->getFid(),
'wijziging'=> $district->getWijziging(),
'nieuwnr' => $district->getNieuwnr(),
'naam' => $district->getNaam(),
'wijk' => $district->getWijk(),
'wijknr' => $district->getWijknr(),
'objectid' => $district->getObjectid(),
'area' => $district->getArea(),
'len' => $district->getLen(),
);
$this->_dbTable->insert($data);
_dbTable->对我的表格“Districts”的引用
现在,我想在插入数据之前先清除表
我如何才能做到这一点?试试:
$this->_dbTable->delete("1=1");
你应该解决你的问题。1=1将匹配所有记录,从而删除它们。据我所知,Zend_Db或PDO中没有截断方法
@Rikesh是对的,花点时间回顾一下,您将得到更好的帮助。如果确实需要截断表,请尝试获取适配器
$this->_dbTable->getAdapter()->query('TRUNCATE TABLE '.$this->_dbTable->info(Zend_Db_Table::NAME));
扩展Zend_Db_Table_Abstract并添加:
/**
* Remove all contents of the table
* @return this
*/
public function truncate()
{
$this->getAdapter()->query('TRUNCATE TABLE `' . $this->_name . '`');
return $this;
}
如果将Zend Framework 2与tableGateway一起使用,则过程非常类似
$query = $this->tableGateway->getAdapter()->query('TRUNCATE TABLE '.$this->tableGateway->getTable());
$query->execute();
对于带tableGateway的Zend Framework 2/3:
$adapter = $this->tableGateway->getAdapter();
$sql = 'TRUNCATE TABLE '.$this->tableGateway->getTable();
//Or,
$sql = 'DELETE FROM '.$this->tableGateway->getTable();
$adapter->query($sql)->execute();
// Or,
$adapter->createStatement($sql)->execute();
当我这样做时:$this->_dbTable->delete(“1=1”)$此->\u dbTable->insert($data);当我检查我的数据库时,我发现只有最后一行…如果你在删除后插入它,它应该在那里?我肯定这会抹掉桌子上的所有东西。您必须在删除后插入。
$truncate = new Truncate('table');
return $this->getSql()->prepareStatementForSqlObject($truncate)->execute();
$adapter = $this->tableGateway->getAdapter();
$sql = 'TRUNCATE TABLE '.$this->tableGateway->getTable();
//Or,
$sql = 'DELETE FROM '.$this->tableGateway->getTable();
$adapter->query($sql)->execute();
// Or,
$adapter->createStatement($sql)->execute();