2013-01-21 47 views
3

我这样做:截断表+ Zend框架

$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 - >我的表 '区' 的引用。

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

我该怎么做?

回答

11

试图让适配器,如果你确实需要截断表

$this->_dbTable->getAdapter()->query('TRUNCATE TABLE '.$this->_dbTable->info(Zend_Db_Table::NAME)); 
+2

+1这通常比'DELETE ... WHERE 1 = 1'快得多。 –

+0

像完美的作品!谢谢! – nielsv

4

尝试:

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

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

@瑞克斯是正确的需要花一些时间审查faq后,你会得到更好的帮助。

+0

当我做到这一点: $本 - > _ dbTable->删除( “1 = 1”); $ this - > _ dbTable-> insert($ data); 当我检查我的数据库时,我发现它只有最后一行... – nielsv

+0

那么它应该在那里,如果你删除后插入它?我认为这会消除桌子上的一切。您必须在删除后插入。 – Iznogood

3

扩展Zend_Db_Table_Abstract并添加:

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

    return $this; 
} 
1

如果您正在使用Zend Framework 2 tableGateway,这个过程是非常类似。

$query = $this->tableGateway->getAdapter()->query('TRUNCATE TABLE '.$this->tableGateway->getTable()); 
    $query->execute(); 
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();