ThinkPHP的CURD易忽视点小结
thinkphp的CURD易忽视点小结
1、使用对象的方法插入数据 D用法。
$Form = D('Form');
$data['title'] = 'ThinkPHP';
$data['content'] = '表单内容';
$Form->add($data);
其实thinkphp还支持对象的方式直接向数据库插入数据,如下:
$Form = D('Form');
$Form->title = 'ThinkPHP';
$Form->content = '表单内容';
$Form->add();
2、不指定条件对数据更新。
$Form = M("Form"); // 要修改的数据对象属性赋值
$data['id'] = 5;
$data['title'] = 'ThinkPHP';
$data['content'] = 'ThinkPHP3.1版本发布';
$Form->save($data); // 根据条件保存修改的数据
save方法会自动识别数据对象中的主键字段,并作为更新条件。当然,你也可以显式的传入更新条件,也就是我们最常用的方法:
$Form = M("Form");
// 要修改的数据对象属性赋值
$data['title'] = 'ThinkPHP';
$data['content'] = 'ThinkPHP3.1版本发布';
$Form->where('id=5')->save($data); // 根据条件保存修改的数据
其实还有对象的方式,就像上面提到的,插入数据,用对象的方式一样:
$Form = M("Form");
// 要修改的数据对象属性赋值
$Form->title = 'thinkphp';
$Form->content = 'ThinkPHP3.1版本发布';
$Form->where('id=5')->save(); // 根据条件保存修改的数据
并且,可以将主键字段包含在要保存的数据里,这样就可以不需要写where了
$Form = M("Form");
// 要修改的数据对象属性赋值
$Form->id = 5;
$Form->title = 'thinkphp';
$Form->content = 'ThinkPHP3.1版本发布';
$Form->save(); // 根据数据对象中的主键保存修改的数据
这里只是一小部分总结,大家想了解更多,可查看http://www.jb100.net/html/content-28-248-1.html,里面写的很详细,对大家很有帮助。
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool