Yii uses DeleteAll to delete a table.

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.