3

我这样做:

$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 -> 对我的表“地区”的引用。

现在我想在插入数据之前先清除表格。

我怎样才能做到这一点?

4

6 回答 6

12

如果您确实需要截断表格,请尝试获取适配器

$this->_dbTable->getAdapter()->query('TRUNCATE TABLE '.$this->_dbTable->info(Zend_Db_Table::NAME));
于 2013-01-21T20:03:18.870 回答
4

尝试:

$this->_dbTable->delete("1=1");

应该照顾你的问题。1=1 将匹配所有记录,从而删除它们。据我所知,Zend_Db 或 PDO 中没有截断方法。

@Rikesh 是正确的花点时间查看常见问题解答后,您将获得更好的帮助。

于 2013-01-21T16:16:44.040 回答
4

扩展 Zend_Db_Table_Abstract 并添加:

/**
 * Remove all contents of the table
 * @return this
 */
public function truncate()
{
    $this->getAdapter()->query('TRUNCATE TABLE `' . $this->_name . '`');

    return $this;
}
于 2014-05-12T09:04:37.740 回答
2

如果您将 Zend Framework 2 与 tableGateway 一起使用,则该过程非常相似。

    $query = $this->tableGateway->getAdapter()->query('TRUNCATE TABLE '.$this->tableGateway->getTable());
    $query->execute();
于 2016-06-21T18:06:39.943 回答
0
<?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();
于 2016-12-13T09:35:25.177 回答
0

对于带有 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();
于 2020-01-23T07:08:36.807 回答