Loading

fastadmin中关联表时A为主表,想让B表和C表关联时怎么办?

$sql = Db::connect('数据库')->table('C表')->where('status', 'normal')->field('字段 别称【不可与其他表重复】')->buildSql();
$total = $this->model
                    ->fetchSql(false)
                    ->with(['B表'])
                    ->join([$sql=>'C表'],'C表关联B表字段 = B表关联C表字段','left')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

上面是自己想的,后来发现了下面这种方法更简单

->with(['B模型'=>['C方法']])
foreach ($list as $row) { 
  $row->visible(['B模型']);
$row->getRelation('B模型')->visible(['C方法']);
$row->seller = ['C字段'=>$row->B模型->C方法->C字段];
}

 

posted @ 2019-09-10 17:27  5572  阅读(2169)  评论(0编辑  收藏  举报