smart 2017-11-26 13:45:48 3519次浏览 0条评论 0 1 0

ActiveRecord 的关联表操作

### // models 中getXxx
// 关联goods表
public function getGoods()
{
    // 一关联一 hasOne
    return $this->hasOne(Status::className(), ['id' => 'jihuo']);
}

// 关联user表 并传值 进一步判断
public function getUser($status = 1)
{
    // 一关联多 hasMany ; p_id 被关联表字段, id 关联表字段(当前表字段)
    return $this->hasMany(User::className(), ['p_id' => 'id'])
                ->where('status = :status', [':status' => $status]);
}

控制器中调用

$user = $customer->getUser(0)->all(); // 传值
$user = $customer->user;    // 不传值
觉得很赞
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册