search
Homephp教程php手册CakePHP 2.x CookBook 中文版 第七章 模型 之 删除数据

删除数据

CakePHP 的模型类提供了几种从数据库中删除记录的方法。

 

delete

delete(int $id = null, boolean $cascade = true);

 

通过 $id 删除记录。默认情况下,同时删除那些依赖于被删除记录的记录。

 

例如,删除一个与许多 Recipe 记录相关的 User 记录(User ‘hasMany’ 或者 ‘hasAndBelongsToMany’ Recipes):

 

如果 $cascade 设置为 true,相关的 Recipe 记录也被删除(模型的 dependent-value 设置为 true)。

如果 $cascade 设置为 false,User 删除后,Recipe 记录仍被保留。

如果数据库支持外键和级联删除,会比 CakePHP 的级联更优先被依赖。使用 Model:delete() 的级联特性的好处之一是它允许你使用行为和模型的回调:

 

1 $this->Comment->delete($this->request->data('Comment.id'));

你可以使用同时存在于模型和行为中的 beforeDelete 和 afterDelete 向删除过程挂接自定义逻辑。更多信息请参见回调方法。

 

deleteAll

deleteAll(mixed $conditions, $cascade = true, $callbacks = false)

 

除了 deleteAll() 将删除所有与提供的条件相匹配的记录之外,它与 delete() 基本一样。$conditions 数组将以 SQL 片段或数组的形式提供。

 

conditions 要匹配的条件

cascade 逻辑值,设置为 true,删除时将同时删除有依赖关系的记录。

callbacks 逻辑值,运行回调

成功时返回 True,失败时挽回 False。

 

示例:

 

1 // 删除所用的条件与 find() 相同

2 $this->Comment->deleteAll(array('Comment.spam' => true), false);

如果删除带有回调 和/或 级联,将查找相关行并删除。这常常会导致发出多条查询。

 

注解

 

当删除条件成功执行但没有记录匹配时,即使没有记录被删除,deleteAll() 也将返回 true。

Statement
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 Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools