Php YII2在连接两个表时未知'with'和'joinWith'

Php YII2在连接两个表时未知'with'和'joinWith',php,yii2,Php,Yii2,我有两个相关的表,我想连接它们,但出现以下错误: 异常“yii\base\UnknownMethodException”,消息为“Calling” 未知方法:backend\models\TblSubProject::joinWith()' 这是关系 public function getBrgyCode() { return $this->hasOne(LibBrgy::className(), ['brgy_code' => 'brgy_code']); } 这里是行动

我有两个相关的表,我想连接它们,但出现以下错误:

异常“yii\base\UnknownMethodException”,消息为“Calling” 未知方法:backend\models\TblSubProject::joinWith()'

这是关系

public function getBrgyCode()
{
    return $this->hasOne(LibBrgy::className(), ['brgy_code' => 'brgy_code']);
}
这里是行动

public function actionGetsp()
{
  $sp_id=$_POST['sp_id'];
if (($sp = TblSubProject::findOne($sp_id)->joinWith(brgyCode)) !== null) {
        return $sp->sp_title."||";
    } else {
        return "Not Found";
    }
}

我通过ajax调用该操作。

findOne
方法返回查询的最终结果-
ActiveRecord
对象

with
joinWith
只能应用于
ActiveQuery
对象

例如,将查询重写为:

$subProject = TblSubProject::find()->joinWith('brgyCode')->one();
您还遗漏了
joinWith
中关系名称周围的引号

官方文件:


从文档中,我找到了这样的解决方案
$customer=customer::findOne(123)$订单=$customer->orders$subProject=TblSubProject::find($sp_id)->joinWith('brgyCode')->one()?这似乎返回了第一个值..在
where
部分中应用附加条件:
$subProject=TblSubProject::find()->joinWith('brgyCode')->where(['sp_id'=>$sp_id])->one()joinWith
with
就足够了。是的,它返回一个结果,对于一对多关系使用
all()
。至于文档中的解决方案,也可以。根据情况使用它。我用
ActiveQuery
显示它,因为它是在您最初的问题中。