Yii uses DeleteAll to delete a table.
The example in this article describes how to solve an error when using DeleteAll to delete a table. We will share this with you for your reference. The details are as follows:
When deleting data, you will often encounter a connection condition to determine whether to delete the data. Today, we use Yii CDbCriteria to generate a connection condition. Database errors are reported during batch deletion.
The page code is:
$criteria=new CDbCriteria;$criteria->join = ' LEFT JOIN {{positions}} p ON p.zpo_id=t.zpo_id ';$criteria->addCondition("p.zpo_type=1");$criteria->addCondition("t.zpl_content_id in ($id)");PosLog::model()->deleteAll($criteria);
Error SQL:
Copy codeThe Code is as follows: delete from 'zd _ pos_log' left join zd_positions p ON p. zpo_id = t. zpo_id WHERE (p. zpo_type = 1) AND (t. zpl_content_id in (76 ))
The correct SQL statement should be:
Copy codeThe Code is as follows: DELETE t FROM 'zd _ pos_log't left join zd_positions p ON p. zpo_id = t. zpo_id WHERE (p. zpo_type = 1) AND (t. zpl_content_id in (76 ))
Back to Yii base class:
Framework/db/schema/CDbCommandBuilder. php #166
public function createDeleteCommand($table,$criteria){ $this->ensureTable($table); $sql="DELETE FROM {$table->rawName}"; $sql=$this->applyJoin($sql,$criteria->join); $sql=$this->applyCondition($sql,$criteria->condition); $sql=$this->applyGroup($sql,$criteria->group); $sql=$this->applyHaving($sql,$criteria->having); $sql=$this->applyOrder($sql,$criteria->order); $sql=$this->applyLimit($sql,$criteria->limit,$criteria->offset); $command=$this->_connection->createCommand($sql); $this->bindValues($command,$criteria->params); return $command;}
Solution. Modify the base class method:
public function createDeleteCommand($table,$criteria,$alias='t'){ $this->ensureTable($table); $alias=$this->_schema->quoteTableName($alias); if(empty($criteria->join)){ $sql="DELETE FROM {$table->rawName}"; }else{ $sql="DELETE $alias FROM {$table->rawName} $alias"; } $sql=$this->applyJoin($sql,$criteria->join); $sql=$this->applyCondition($sql,$criteria->condition); $sql=$this->applyGroup($sql,$criteria->group); $sql=$this->applyHaving($sql,$criteria->having); $sql=$this->applyOrder($sql,$criteria->order); $sql=$this->applyLimit($sql,$criteria->limit,$criteria->offset); $command=$this->_connection->createCommand($sql); $this->bindValues($command,$criteria->params); return $command;}