欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Yii使用DeleteAll連表刪除出現(xiàn)報錯問題的解決方法

 更新時間:2016年07月14日 09:48:41   作者:dreamzml  
這篇文章主要介紹了Yii使用DeleteAll連表刪除出現(xiàn)報錯問題的解決方法,分析了相關(guān)的SQL語句及出現(xiàn)錯誤的原因與改正方法,需要的朋友可以參考下

本文實例講述了Yii使用DeleteAll連表刪除出現(xiàn)報錯問題的解決方法。分享給大家供大家參考,具體如下:

刪除數(shù)據(jù)的時候,經(jīng)常會遇到連聯(lián)判斷刪除數(shù)據(jù)的條件,今天用Yii 的CDbCriteria生成關(guān)連條件。批量刪除的時候數(shù)據(jù)庫報錯。

頁面代碼為:

$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);

錯誤SQL為:

復(fù)制代碼 代碼如下:
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))

正確SQL語句應(yīng)為:

復(fù)制代碼 代碼如下:
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))

追溯到Y(jié)ii 基類:

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

解決方案。修改基類方法:

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

更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論