Home Backend Development PHP Tutorial Yii solves DeleteAll connection table deletion error reporting problem

Yii solves DeleteAll connection table deletion error reporting problem

Jan 02, 2018 pm 01:05 PM
Report an error question

This article mainly introduces the solution to the error problem when Yii uses DeleteAll to delete concatenated tables. It analyzes the relevant SQL statements and the causes and correction methods of the errors. Friends in need can refer to it. I hope to be helpful.

The details are as follows:

When deleting data, we often encounter the conditions for determining the deletion of data. Today, we use Yii's CDbCriteria to generate related conditions. The database reports an error when deleting in batches.

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);
Copy after login

The error SQL is:

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:

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

Trace back to the 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;
}
Copy after login

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;
}
Copy after login

Related recommendations:

##Yii2 implements rbac permission control

Detailed explanation of Yii operating mechanism and routing

How to load Yii’s own verification code function

The above is the detailed content of Yii solves DeleteAll connection table deletion error reporting problem. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Cause analysis: HTTP request error 504 gateway timeout Cause analysis: HTTP request error 504 gateway timeout Feb 19, 2024 pm 05:12 PM

Brief introduction to the reason for the http request error: 504GatewayTimeout: During network communication, the client interacts with the server by sending HTTP requests. However, sometimes we may encounter some error messages during the process of sending the request. One of them is the 504GatewayTimeout error. This article will explore the causes and solutions to this error. What is the 504GatewayTimeout error? GatewayTimeo

Solution: Socket Error when handling HTTP requests Solution: Socket Error when handling HTTP requests Feb 25, 2024 pm 09:24 PM

http request error: Solution to SocketError When making network requests, we often encounter various errors. One of the common problems is SocketError. This error is thrown when our application cannot establish a connection with the server. In this article, we will discuss some common causes and solutions of SocketError. First, we need to understand what Socket is. Socket is a communication protocol that allows applications to

Step-by-step guide to quickly solve pyqt5 installation errors Step-by-step guide to quickly solve pyqt5 installation errors Jan 19, 2024 am 09:32 AM

If you are learning Python and want to develop GUI applications, PyQt5 is a very good choice. It is a bound version of the PyQt library under Python, which makes it very convenient to call and develop the Qt graphics library. However, sometimes you may encounter some problems when installing PyQt5. This guide will provide you with some steps to quickly solve installation error problems, and also attach specific code examples. Make sure the Python version is correct PyQt5 is a Python-based library, so first

Teach you how to diagnose common iPhone problems Teach you how to diagnose common iPhone problems Dec 03, 2023 am 08:15 AM

Known for its powerful performance and versatile features, the iPhone is not immune to the occasional hiccup or technical difficulty, a common trait among complex electronic devices. Experiencing iPhone problems can be frustrating, but usually no alarm is needed. In this comprehensive guide, we aim to demystify some of the most commonly encountered challenges associated with iPhone usage. Our step-by-step approach is designed to help you resolve these common issues, providing practical solutions and troubleshooting tips to get your equipment back in peak working order. Whether you're facing a glitch or a more complex problem, this article can help you resolve them effectively. General Troubleshooting Tips Before delving into specific troubleshooting steps, here are some helpful

http request error: 504 Gateway Timeout solution http request error: 504 Gateway Timeout solution Feb 22, 2024 am 08:27 AM

Title: http request error: 504GatewayTimeout solution introduction: During the process of making network requests, sometimes you may encounter http request errors. One of the common errors is the 504GatewayTimeout error. This article will introduce the meaning and possible causes of the 504GatewayTimeout error, and provide some methods to solve the error. Text: What does the 504GatewayTimeout error mean?

How to deal with Laravel API error problems How to deal with Laravel API error problems Mar 06, 2024 pm 05:18 PM

Title: How to deal with Laravel API error problems, specific code examples are needed. When developing Laravel, API errors are often encountered. These errors may come from various reasons such as program code logic errors, database query problems, or external API request failures. How to handle these error reports is a key issue. This article will use specific code examples to demonstrate how to effectively handle Laravel API error reports. 1. Error handling in Laravel

Solve the problem of UniApp error: unable to find 'xxx' animation effect Solve the problem of UniApp error: unable to find 'xxx' animation effect Nov 25, 2023 am 11:43 AM

Solve the problem of UniApp error: 'xxx' animation effect cannot be found UniApp is a cross-platform application development framework based on the Vue.js framework, which can be used to develop applications for multiple platforms such as WeChat applets, H5, and App. During the development process, we often use animation effects to improve user experience. However, sometimes you will encounter an error: The 'xxx' animation effect cannot be found. This error will cause the animation to fail to run normally, causing inconvenience to development. This article will introduce several ways to solve this problem.

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

See all articles