model
Banner
BannerItem
<?php
namespace app\api\model;
use think\Db;
use think\Exception;
use think\Model;
//Banner代码
class Banner extends Model
{
public function items(){
return $this->hasMany('BannerItem','banner_id','id');
}
}
<?php
namespace app\api\model;
use think\Model;
//BannerItem 代码
class BannerItem extends Model
{
//
}
在controller中进行调用
<?php
namespace app\api\controller\v1;
use app\api\model\Banner as BannerModel;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\BannerMissException;
use think\Exception;
class Banner
{
/**
* 获取指定id的banner信息
* @url/banner/:id
* @http GET
* @id banner的id号
*
*/
public function getBanner($id){
$validate = new IDMustBePositiveInt();
$validate->goCheck();
//进行调用
$banner = BannerModel::with('items')->find($id);
if(!$banner){
throw new BannerMissException();
}
return $banner;
//不用json()序列化,tp5直接会序列化,但是是别的不是json而是text/html
}
}
查询结果
{
"id": 1,
"name": "首页置顶",
"description": "首页轮播图",
"delete_time": null,
"update_time": "1970-01-01 08:00:00",
"items": [
{
"id": 1,
"img_id": 65,
"key_word": "6",
"type": 1,
"delete_time": null,
"banner_id": 1,
"update_time": "1970-01-01 08:00:00"
},
{
"id": 2,
"img_id": 2,
"key_word": "25",
"type": 1,
"delete_time": null,
"banner_id": 1,
"update_time": "1970-01-01 08:00:00"
},
{
"id": 3,
"img_id": 3,
"key_word": "11",
"type": 1,
"delete_time": null,
"banner_id": 1,
"update_time": "1970-01-01 08:00:00"
},
{
"id": 5,
"img_id": 1,
"key_word": "10",
"type": 1,
"delete_time": null,
"banner_id": 1,
"update_time": "1970-01-01 08:00:00"
}
]
}