php根据数据库隐藏,ThinkPHP5.1关联模型字段(包括非数据库字段)的显示和隐藏...

之前使用字段显示用得是field来进行限制,但是关联使用field的时候必须要传入关联字段,然后就想到了使用visible来进行补充,这个时候又发现一个问题,使用了with以后,就算visible不指定关联模型字段,结果中也是有关联模型字段的,最终经过测试发现,如果希望使用visible进行限制关联模型字段,则需要详细的指定关联模型中的字段。

数据表

用户表user

id

username

1

swk

2

zbj

个人资料表profile

user模型,代码如下:

namespace app\common\model;

use think\Model;

class User extends Model

{

// 关联profile模型

public function profile()

{

return $this->hasOne(Profile::class, 'uid');

}

}

profile模型,代码如下

namespace app\common\model;

use think\Model;

class Profile extends Model

{

// 非数据库字段age(获取器)

public function getAgeAttr($value, $info)

{

return 22;

}

你可能感兴趣的:(php根据数据库隐藏)