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

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

数据表

用户表user

id username
1 swk
2 zbj

个人资料表profile

id uid email
1 2 1@dream.cn
2 1 2@dream.cn

user模型,代码如下:

<?php

namespace app\common\model;

use think\Model;

class User extends Model
{
    // 关联profile模型
    public function profile()
    {
        return $this->hasOne(Profile::class, 'uid');
    }
}

profile模型,代码如下

<?php

namespace app\common\model;

use think\Model;

class Profile extends Model
{
    // 非数据库字段age(获取器)
    public function getAgeAttr($value, $info)
    {
        return 22;
    }
}

需求如下

  1. 需要user模型的username
  2. 需要profile模型的email字段
  3. 需要profile模型的非数据库字段age

第一次尝试

不限制任何字段进行查询,代码如下:

<?php
namespace app\index\controller;

use app\common\model\User;

class Index
{
    public function test()
    {
        $userInfo = User::with(['profile'])->find();
        halt($userInfo);
    }
}

结果如下:


image

分析:需要把俩个id和uid去掉,然后加上age

第二次尝试

通过field限制字段,代码如下:

<?php
namespace app\index\controller;

use app\common\model\User;

class Index
{
    public function test()
    {
        $userInfo = User::with(['profile' => function ($query) {
            $query->field(['email']);
        }])->field(['username'])->find();
        halt($userInfo);
    }
}

这个时候报错了,查询文档发现with使用field的时候需要加上关联字段的,报错如下:


image

第三次尝试

根据文档加上关联字段,代码如下:

<?php
namespace app\index\controller;

use app\common\model\User;

class Index
{
    public function test()
    {
        $userInfo = User::with(['profile' => function ($query) {
            $query->field(['uid', 'email']);
        }])->field(['id', 'username'])->find();
        halt($userInfo);
    }
}

执行结果如下:


image

第四次尝试

既然field无法限制id和uid字段,那么我们使用visible,代码如下

<?php
namespace app\index\controller;

use app\common\model\User;

class Index
{
    public function test()
    {
        $userInfo = User::with(['profile' => function ($query) {
            $query->field(['uid', 'email']);
        }])->field(['id', 'username'])->find()->visible(['username']);
        halt($userInfo);
    }
}

这个时候我们发现visible中没有设置关联模型字段但是,关联模型字段还是出现在结果中了,我们再一次限制关联模型字段试试,代码如下:

<?php
namespace app\index\controller;

use app\common\model\User;

class Index
{
    public function test()
    {
        $userInfo = User::with(['profile' => function ($query) {
            $query->field(['uid', 'email']);
        }])->field(['id', 'username'])->find()->visible(['username', 'profile.email']);
        halt($userInfo);
    }
}

结果如下


image

最后就剩下非数据库字段的显示了,手册中已经明确告诉我们要使用append来显示的得到非数据库字段的值,虽然append也不受visible的限制,但是建议大家把需要用到的所有字段统一写到visible中,方便我们维护,这样我们就可以很明确的看到我们最终需要输出那些字段,最终代码如下:

<?php
namespace app\index\controller;

use app\common\model\User;

class Index
{
    public function test()
    {
        $userInfo = User::with(['profile' => function ($query) {
            $query->field(['uid', 'email'])->append(['age']);
        }])->field(['id', 'username'])->find()
            ->visible(['username', 'profile.age', 'profile.email']);
        halt($userInfo);
    }
}

执行结果如下:


image

如果大家有更好的方法,欢迎交流。邮箱leruge@163.com

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容