Home  >  Article  >  PHP Framework  >  thinkphp6 model does not return ID

thinkphp6 model does not return ID

王林
王林Original
2023-05-26 11:45:37820browse

Today I encountered a problem when using the thinkphp6 model, that is, the model did not return the ID when creating data. This confuses me very much, because in my previous development experience, the ID is usually returned when creating data for subsequent operations. In this post, I'll share how I discovered the cause of this problem and how to fix it.

First, I checked my code to make sure I was using the model creation method correctly. My code is as follows:

$user = new User();
$user->name = 'John';
$user->email = 'john@example.com';
$user->save();

return $user->id;

This code should look fine because it calls the model's save() method, which should return the ID of a newly created data row. However, when I run the code, I find that the ID returned is 0, not what I expected. This confuses me very much because I've been using the thinkphp framework for a long time and I've never had this problem.

Next, I looked at the source code of the model, especially the implementation of the save() method. I found that the model's save() method is implemented by calling the create() method to create a new data row before saving it:

public function save($data = [], $where = [], $sequence = null)
{
    if ($this->isExists()) {
        return $this->update($data, $where, $sequence);
    } else {
        $result = $this->create($data);
        if ($result) {
            $this->sync($result);
        }
        return $result;
    }
}

It is obvious that when the data is created, the model calls create() method. Then, I will check the implementation of the create() method to see if it correctly returns the ID of the newly created data row.

public function create($data = [], $sequence = null)
{
    if (empty($data)) {
        $data = $this->getOriginData();
    }
    $result = $this->db()->insertGetId($data, $sequence);
    if (!empty($result)) {
        $this->exists(true);
        $this->setRawData([], true);
        $this->sync($result);
        $this->trigger('after_create');
        return $result;
    } else {
        return false;
    }
}

By carefully observing the code of the create() method, I found that it does return the ID of the newly created data row. So what's the problem?

Finally, I checked the table structure of the database and checked whether the ID field in the table was correctly set as an auto-increment column. As a result, I found that the problem lies here: my ID field is not set as an auto-increment column.

This question is both ridiculous and embarrassing because the reason is so simple. However, it also illustrates the fact that we need to carefully check the database table structures when using models to ensure that they are configured correctly. Only in this way can we use the model correctly and avoid unnecessary problems.

In my case, I just set the ID field to an auto-increment column and re-ran my code to correctly return the ID of the newly created data row. After this problem was solved, I realized that the ID value returned when creating data was based on the auto-increment column feature of the database.

In this post, I share my experience of how I discovered a small problem. The root cause of this problem is that the table structure of the database is not configured correctly, resulting in the model not returning ID when creating data. By carefully checking the code, looking at the model source code, and checking the database table structure, I finally solved the problem. I hope my experience can help other developers better understand the thinkphp6 framework model so that they can better use this powerful tool to develop web applications.

The above is the detailed content of thinkphp6 model does not return ID. 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