Home >Web Front-end >JS Tutorial >How to solve the problem of the field with the same name in hasOne in thinkphp model

How to solve the problem of the field with the same name in hasOne in thinkphp model

一个新手
一个新手Original
2018-05-23 16:49:122895browse

There are two tables: play table and type table

play table fields:

id type

type table field:

id title

##type in the play table and id in the type table association.

The model in thinkphp5 is defined as follows:

play model:

class Play extends Model
{
    protected $table = 'wx_play';

    public function type2()
    {
        return $this->hasOne("Type", "id", "type");
    }

type model:

class Type extends Model
{
    protected $table = 'wx_type';
}

Note:

The type2 function in the play model cannot be written as type, otherwise it will be different from the type field in the play table The conflict results in only the fields in the play table being queried, not the objects in the type table.

According to thinkphp5 documentation:

Tips: The type2 method of the Play model is an association definition method, and the method name can be arbitrary Name it, but be careful to avoid conflict with the field attributes of the Play model object
.

The above is the detailed content of How to solve the problem of the field with the same name in hasOne in thinkphp model. 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