用户资金记录表和用户表,两张表通过userId关联
需求:需要在资金记录表中添加两列,username用户名和realname真实姓名,并实现自动搜索
直接干货
第一步:找到用户资金记录表对应的AR模型类 ,common\models\BorrowAccountLog,在该文件中进行关联User表
public function getUser(){ return $this->hasOne(User::className(),['userId'=>'userId']); }
自己百度下hasOne
第二步:在BorrowAccountLogSearch中加入
public $username;
public $realname;
rules()方法中加入下面规则:
[['username','realname'],'safe'],//一定要加,不然搜索框出不来
search()方法中加入:
$query->joinWith(['user']); $query->select("hs_borrow_account_log.*, hs_user.username, hs_user.realname");