Home  >  Article  >  Backend Development  >  Some summary about adding, deleting, modifying and checking in ThinkPHP

Some summary about adding, deleting, modifying and checking in ThinkPHP

jacklove
jackloveOriginal
2018-06-15 10:39:231616browse

Today I learned some operations on adding, deleting, modifying, and checking in ThinkPHP. I feel that the writing is quite clear. Let’s learn it together!

1. Creation operation

Use the add method in ThinkPHP to add data to the database.

The usage method is as follows:

$User = M("User"); // 实例化User对象
$data['name'] = 'ThinkPHP';
$data['email'] = 'ThinkPHP@gmail.com';
$User->add($data);

Or use the data method to operate continuously

$User->data($data)->add();

If the data object has been created before add (for example, using the create or data method), the add method does not need to pass in data.

Example of using the create method:

$User = M("User"); // 实例化User对象
// 根据表单提交的POST数据创建数据对象
$User->create();
$User->add(); // 根据条件保存修改的数据

If your primary key is automatically growing type, and if the data is inserted successfully, the return value of the Add method is the latest inserted primary key value, which can be obtained directly.

2. Reading data

There are many ways to read data in ThinkPHP, usually divided into reading data and read datasets.

To read the data set, use the findall or select method (findall and select methods are equivalent):

$User = M("User"); // 实例化User对象
// 查找status值为1的用户数据以创建时间排序返回10条数据
$list = $User->where('status=1')->order('create_time')->limit(10)->select();

The return value of the select method is a two-dimensional array. If no results are found, an empty array is returned. Combined with the coherent operation method mentioned above, complex data queries can be completed. The most complex coherent method should be the use of the where method. Because this part involves a lot of content, we will use it in detail in the query language part on how to assemble query conditions. illustrate. The basic query does not involve the related query part for the time being, but uses the related model to perform data operations. Please refer to the related model part for this part.

Use the find method to read data:

The operation of reading data is actually similar to that of the data set. Select all the coherent operation methods available. They can also be used in the find method. The difference is that the find method will only return at most one record, so the limit method is invalid for the find query operation.

$User = M("User"); // 实例化User对象
// 查找status值为1name值为think的用户数据
$User->where('status=1 AND name="think"
 ')->find();

Even if there is more than one piece of data that meets the conditions, the find method will only return the first record.

If you want to read the value of a field, you can use the getField method, for example:

$User = M("User"); // 实例化User对象
// 获取ID为3的用户的昵称
$nickname = $User->where('id=3')->getField('nickname');

When there is only one field, always return a value.

If multiple fields are passed in, an associative array can be returned:

$User = M("User"); // 实例化User对象
// 获取所有用户的ID和昵称列表
$list = $User->getField('id,nickname');

The returned list is an array, the key name is the user's id, and the key value is the user's nickname.

3. Update data

Use the save method in ThinkPHP to update the database, and The use of coherent operations is also supported.

$User = M("User"); // 实例化User对象
// 要修改的数据对象属性赋值
$data['name'] = 'ThinkPHP';
$data['email'] = 'ThinkPHP@gmail.com';
$User->where('id=5')->save($data); // 根据条件保存修改的数据

In order to ensure the security of the database and avoid errors, update the entire data table. If there are no update conditions, the data object itself will not contain For primary key fields, the save method will not update any database records.

Therefore the following code will not change any records in the database

$User->save($data); 

除非使用下面的方式:

$User = M("User"); // 实例化User对象
// 要修改的数据对象属性赋值

$data['id'] = 5;
$data['name'] = 'ThinkPHP';
$data['email'] = 'ThinkPHP@gmail.com';
$User->save($data); // 根据条件保存修改的数据

如果id是数据表的主键的话,系统自动会把主键的值作为更新条件来更新其他字段的值。

还有一种方法是通过create或者data方法创建要更新的数据对象,然后进行保存操作,这样save方法的参数可以不需要传入。

$User = M("User"); // 实例化User对象
// 要修改的数据对象属性赋值
$data['name'] = 'ThinkPHP';
$data['email'] = 'ThinkPHP@gmail.com';
$User->where('id=5')->data($data)->save(); // 根据条件保存修改的数据

使用create方法的例子:

$User = M("User"); // 实例化User对象
// 根据表单提交的POST数据创建数据对象
$User->create();
$User->save(); //根据条件保存要修改的数据

上面的情况,表单必须包含一个以主键为名称的隐藏域,才能完成保存操作。

如果只是更新个别字段的值,可以使用setField方法:

$User = M("User"); // 实例化User对象
// 更改用户的name值
$User-> where('id=5')->setField('name','ThinkPHP');
setField方法支持同时更新多个字段,只需要传入数组即可,例如:
$User = M("User"); // 实例化User对象
// 更改用户的name和email的值
$User-> where('id=5')->setField(array('name','email'),array('ThinkPHP','ThinkPHP@gmail.com'));

而对于统计字段(通常指的是数字类型)的更新,系统还提供了setIncsetDec方法:

$User = M("User"); // 实例化User对象
$User->setInc('score','id=5',3);// 用户的积分加3
$User->setInc('score','id=5'); // 用户的积分加1
$User->setDec('score','id=5',5);// 用户的积分减5
$User->setDec('score','id=5'); // 用户的积分减1

四、删除数据

在ThinkPHP使用delete方法删除数据库的记录。同样可以使用连贯操作进行删除操作。

$User = M("User"); // 实例化User对象
$User->where('id=5')->delete(); // 删除id为5的用户数据
$User->where('status=0')->delete(); // 删除所有状态为0的用户数据

delete方法可以用于删除单个或者多个数据,主要取决于删除条件,也就是where方法的参数,也可以用orderlimit方法来限制要删除的个数,例如:

// 删除所有状态为0的5个用户数据按照创建时间排序
$User->where('status=0')->order('create_time')->limit('5')->delete();

本文讲解了关于ThinkPHP的增、删、改、查 的一些总结,更多相关内容请关注php中文网。

相关推荐:

where方法的应用讲解

ThinkPHP 双重循环遍历输出 的相关内容

ThinkPHP5快速入门 方法的介绍

The above is the detailed content of Some summary about adding, deleting, modifying and checking in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

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