1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //跟据主题返回theme主题表image,himage两个字段信息以及deal相应的商品信息列表 public function getThemeInfo( $themeId ){ $theme =model( 'Theme' )->get( $themeId ); $result = $theme ->deals() ->select(); echo $this ->getLastSql(); exit (); // ->where('theme_status',1) // ->select(); // // dump($result);exit(); // return $theme; } //定义theme and deal关联关系 public function deals(){ return $this ->hasMany( 'deal' , 'theme_id' , 'id' ); } |
控制层代码
1 2 3 4 5 6 7 8 9 10 11 12 | public function getComplexOne( $themeId ){ // (new IDMustBePositiveInt())->goCheck(); $theme = model( "Theme" )->getThemeInfo( $themeId ); if ( $theme ){ return $theme ; } else { throw new MissException([ 'msg' => '请求theme商品不存在' , 'errorCode' => 40000 ]); } |
调试结果,
1 | SELECT * FROM `o2o_deal` WHERE `theme_id` = 12 |
老师,我在model层定义了了theme与deal表是一对多的关系,怎样把这两个属性值绑定到模型关系上?