Home  >  Article  >  PHP Framework  >  How to modify the database in thinkphp

How to modify the database in thinkphp

WBOY
WBOYforward
2023-05-27 10:04:221130browse

1. ThinkPHP database operation

In ThinkPHP, we can operate the database through the database operation classes it provides. Commonly used database operation classes are:

  1. Db class

In ThinkPHP, we can use the Db class to add and delete the database. , modify, check and other operations. Examples of its use are as follows:

<?php
use think\Db;

// 查询数据
$list = Db::table(&#39;user&#39;)->where(&#39;id&#39;, 1)->find();

// 新增数据
$data[&#39;name&#39;] = &#39;test&#39;;
$data[&#39;age&#39;] = 20;
Db::table(&#39;user&#39;)->insert($data);

// 更新数据
$where[&#39;id&#39;] = 1;
$data[&#39;name&#39;] = &#39;test&#39;;
$data[&#39;age&#39;] = 30;
Db::table(&#39;user&#39;)->where($where)->update($data);

// 删除数据
$where[&#39;id&#39;] = 1;
Db::table(&#39;user&#39;)->where($where)->delete();
  1. Model class

In ThinkPHP, the Model class inherits from the Db class, so the Model class can use the Db class All methods, but also provides some more convenient methods. Examples of its use are as follows:

<?php
namespace app\index\model;

use think\Model;

class User extends Model
{
    // 查询数据
    public function getUserById($id)
    {
        return $this->where(&#39;id&#39;, $id)->find();
    }

    // 更新数据
    public function updateUser($id, $name)
    {
        return $this->save([&#39;name&#39; => $name], [&#39;id&#39; => $id]);
    }
}

2. How to modify the database with ThinkPHP

When using ThinkPHP to modify the database, you usually go through the following steps:

  1. Create model

We need to create a model that corresponds to the database table we need to access. Since ThinkPHP adopts the MVC design pattern, we need to inherit the Model class from ThinkPHP when creating the model. The following is an example of creating a User model:

<?php
namespace app\index\model;

use think\Model;

class User extends Model
{
    protected $table = &#39;user&#39;;
    protected $pk = &#39;id&#39;;
}

When creating the User model, we specified that the database table corresponding to the model is the user table, and the primary key of the table is id.

  1. Instantiate the model

Next, we need to use the previously created User model to instantiate it and use this model to operate the database. Here is an example of instantiating the User model:

<?php
$userModel = new \app\index\model\User();
  1. Modify data

Once you create an instance of the User model, you can leverage the The provided functions make modifications to the database. The following is an example of using the User model to modify data:

<?php
$userModel = new \app\index\model\User();

// 更新数据
$where['id'] = 1;
$data['name'] = 'test';
$data['age'] = 30;
$userModel->where($where)->update($data);

In the above example, we use the update() method of $UserModel to modify the data with id 1 in the User table, and change the row of data. Change the name field to test and the age field to 30.

The above is the detailed content of How to modify the database in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete