hyperf 数据库模型关系

一对一

控制器 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\User;

/**
 * @AutoController();
 */
class IndexController
{
        public function index(){
                $role = User::query()->find(1)->role;
                return $role->toArray();
        }
}

User模型 app/Model/User.php

<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 */
class User extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    /**
     * 不自动维护时间戳
     * @var bool
     */
    public  $timestamps = false;

    public function role()
    {
        //id字段是role表id, role_id是user表role_id
        return $this->hasOne(Role::class, 'id', 'role_id');
    }
}

Role模型 app/Model/Role.php

<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 */
class Role extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'role';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];
}

user表数据

mysql> select * from user;
+----+-------------+------+---------+--------+
| id | name        | age  | role_id | status |
+----+-------------+------+---------+--------+
|  1 | xiaohong    |   24 |       1 |      1 |
|  2 | huyongjian2 |   24 |       2 |      0 |
|  4 | xiaoming    |   28 |       2 |      1 |
|  5 | xiaoming    |   30 |       2 |      1 |
|  6 | huyongjian1 |   30 |       2 |      1 |
|  7 | huyongjian2 |   31 |       2 |      1 |
|  8 | xiaohong    |   24 |       1 |      1 |
+----+-------------+------+---------+--------+
7 rows in set (0.00 sec)

role表数据

mysql> select * from role;
+----+------------+--------+
| id | name       | status |
+----+------------+--------+
|  1 | super_user |      1 |
|  2 | admin      |      1 |
+----+------------+--------+
2 rows in set (0.00 sec)

访问测试

curl 118.195.173.53:9501/index/index

测试结果

{
    "id": 1,
    "name": "super_user",
    "status": 1
}

一对多模型关系

控制器 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\User;

/**
 * @AutoController();
 */
class IndexController
{
        public function index(){
                $books = User::query()->where('id',1)->find(1)->books;
                return $books->toArray();
        }
}

User模型 app/Model/User.php

<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 */
class User extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    /**
     * 不自动维护时间戳
     * @var bool
     */
    public  $timestamps = false;

    /**
     * 一对多模型关系演示
     */
    public function books()
    {
        //user_id是book表user_id,id是user表id
        return $this->hasMany(Book::class, 'user_id', 'id');
    }
}

Book模型 app/Model/Book.php

<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 */
class Book extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'book';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];
}

user表数据

mysql> select * from user;
+----+-------------+------+---------+--------+
| id | name        | age  | role_id | status |
+----+-------------+------+---------+--------+
|  1 | xiaohong    |   24 |       1 |      1 |
|  2 | huyongjian2 |   24 |       2 |      0 |
|  4 | xiaoming    |   28 |       2 |      1 |
|  5 | xiaoming    |   30 |       2 |      1 |
|  6 | huyongjian1 |   30 |       2 |      1 |
|  7 | huyongjian2 |   31 |       2 |      1 |
|  8 | xiaohong    |   24 |       1 |      1 |
+----+-------------+------+---------+--------+
7 rows in set (0.00 sec)

book表数据

mysql> select * from book;
+----+-------+---------+--------+
| id | name  | user_id | status |
+----+-------+---------+--------+
|  1 | book1 |       1 |      1 |
|  2 | book2 |       2 |      1 |
|  3 | book3 |       1 |      1 |
|  4 | book4 |       1 |      1 |
|  5 | book5 |       1 |      1 |
+----+-------+---------+--------+
5 rows in set (0.00 sec)

访问测试

curl 118.195.173.53:9501/index/index

测试结果

[{
    "id": 1,
    "name": "book1",
    "user_id": 1,
    "status": 1
}, {
    "id": 3,
    "name": "book3",
    "user_id": 1,
    "status": 1
}, {
    "id": 4,
    "name": "book4",
    "user_id": 1,
    "status": 1
}, {
    "id": 5,
    "name": "book5",
    "user_id": 1,
    "status": 1
}]
posted @ 2021-09-28 19:33  胡勇健  阅读(305)  评论(0)    收藏  举报