尝试将数据插入Yii2中的多个表时出错

尝试将数据插入Yii2中的多个表时出错,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,当我试图显示相关表中的数据时,我得到一个错误调用成员函数isAttributeRequired(),该函数为null 我有表:PromoCode、subscribebprice和promotosubscribeeprice。 但是当我试图在促销代码中显示table Sub时,我得到了一个错误 我需要以下链接:在创建表PromoCode时,我们可以从表subscribebeprice中选择数据(select2),我们在那里选择的数据只存储在表SubscribeToPromoCode中 目前,当我试

当我试图显示相关表中的数据时,我得到一个错误
调用成员函数isAttributeRequired(),该函数为null

我有表:
PromoCode
subscribebprice
promotosubscribeeprice
。 但是当我试图在促销代码中显示table Sub时,我得到了一个错误

我需要以下链接:在创建表
PromoCode
时,我们可以从表
subscribebeprice
中选择数据(select2),我们在那里选择的数据只存储在表
SubscribeToPromoCode

目前,当我试图传递一个属性时,我得到NULL

我在所有表中都有多对多连接

型号:
PromoCode

public function getPromoToSubscribePrice()
{
    return $this->hasMany(SubscribePrice::class, ['id' => 'id'])
    ->viaTable('promo_to_subscribe_price', ['promo_id' => 'id']);
}
控制器:
PromoCodeController

public function actionCreate()
{
    $model = new PromoCode();
    $price = SubscribePrice::find();
    $PromoToSubscribePrice = new PromoToSubscribePrice();

    $PromoToSubscribePrice->promo_id = $model->id;
    $PromoToSubscribePrice->price_id = $model->article_id;
    $PromoToSubscribePrice->setAttributes(Yii::$app->request->post());
    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
            'promo' => $price,
        ]);
    }
}
视图

<?= $form->field($promo, 'description')->widget(Select2::className(), [
    'data' => SubscribePrice::find()->orderBy('currency'),
    'options' => [
        'placeholder' => 'Select contributors ...',
        'multiple' => true
    ],
    'pluginOptions' => [
        'allowClear' => true
    ],
]); ?>

更新

除了前面确定的问题外,您在
选择2
数据
选项中也有相同的错误

'data' => SubscribePrice::find()->orderBy('currency'),
您也在那里传递
ActiveRecord
实例,而您需要传递一个数组,该数组应该是
name=>value

'data' => \yii\helpers\ArrayHelper::map(SubscribePrice::find()->orderBy('currency')->all(),'id','currency')
用于将列提取为关联数组并在select2中列出


您的问题在
actionCreate()的第二行

由于
$price
保存的是ActiveRecord实例而不是
model
实例,
$price
作为
promo
在此行中传递给视图

return $this->render('create', 
    [
        'model' => $model,
        'promo' => $price,
    ]
);

在ActiveForm字段population
中进一步使用了这个选项。问题是我已经尝试过这个选项,但结果恰恰是相同的same@Sergej我简直不敢相信,请添加完整的错误跟踪。@Sergej您还有一行
SubscribePrice::find()->orderBy('currency')
这是不正确的
数据
选项需要一个数组,而不是
ActiveRecord的实例
请参见我答案中的更新部分。结果保持不变。我更新了问题(添加的日志),您可以使用
var\u dump($price)
检查模型实例是否在调用
$this->render()之前添加它吗
$price = SubscribePrice::find();
return $this->render('create', 
    [
        'model' => $model,
        'promo' => $price,
    ]
);
$price = new SubscribePrice();
$price = SubscribePrice::find()->where(['column'=>$value])->limit(1)->one();