Reputation: 6800
I do this:
$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 -> references to my table 'Districts'.
Now I want to clear the table first before I insert the data.
How can I do this?
Upvotes: 3
Views: 7295
Reputation: 2505
In case of Zend Framework 2/3 with tableGateway:
$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();
Upvotes: 0
Reputation: 988
<?php
namespace MyNamespace\Db\Sql;
use Zend\Db\Adapter\ParameterContainer;
use Zend\Db\Adapter\Platform\PlatformInterface;
use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Sql\AbstractPreparableSql;
use Zend\Db\Sql\TableIdentifier;
class Truncate extends AbstractPreparableSql
{
/**@#+
* @const string
*/
const SPECIFICATION_TRUNCATE = 'truncate';
/**@#-*/
/**
* @var string[]
*/
protected $specifications = [
self::SPECIFICATION_TRUNCATE => /* @lang SQL */ 'TRUNCATE TABLE %1$s',
];
/**
* @var string|TableIdentifier
*/
protected $table = '';
/**
* @param null|string|TableIdentifier $table
*/
public function __construct($table = null)
{
if ($table) {
$this->table($table);
}
}
/**
* @param string|TableIdentifier $table
* @return self
*/
public function table($table)
{
$this->table = $table;
return $this;
}
/**
* @param PlatformInterface $platform
* @param DriverInterface|null $driver
* @param ParameterContainer|null $parameterContainer
* @return string
*/
protected function processTruncate(PlatformInterface $platform, DriverInterface $driver = null, ParameterContainer $parameterContainer = null)
{
return sprintf(
$this->specifications[static::SPECIFICATION_TRUNCATE],
$this->resolveTable($this->table, $platform, $driver, $parameterContainer)
);
}
}
$truncate = new Truncate('table');
return $this->getSql()->prepareStatementForSqlObject($truncate)->execute();
Upvotes: 0
Reputation: 21
If you are using Zend Framework 2 with tableGateway, the process is very similar.
$query = $this->tableGateway->getAdapter()->query('TRUNCATE TABLE '.$this->tableGateway->getTable());
$query->execute();
Upvotes: 2
Reputation: 1057
Extend Zend_Db_Table_Abstract and add:
/**
* Remove all contents of the table
* @return this
*/
public function truncate()
{
$this->getAdapter()->query('TRUNCATE TABLE `' . $this->_name . '`');
return $this;
}
Upvotes: 4
Reputation: 11340
Try to get the adapter if you really need to truncate the table
$this->_dbTable->getAdapter()->query('TRUNCATE TABLE '.$this->_dbTable->info(Zend_Db_Table::NAME));
Upvotes: 12
Reputation: 12843
Try:
$this->_dbTable->delete("1=1");
Should take care of your problem. 1=1 will match all records thus deleting them. There is no truncate method in Zend_Db or PDO as far as I know.
@Rikesh is right take a moment to review the faq you will get much better help after following that.
Upvotes: 4