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

Error while trying to insert data into several tables in Yii2

当我尝试显示来自相关 table 的数据时,出现错误 Call to a member function isAttributeRequired() on null

我有 tables:PromoCodeSubscribePricePromoToSubscribePrice。 但是,当我尝试在促销代码中显示 table Sub 时,出现错误。

我需要以下 link:当创建 table PromoCode 时,我们可以从 table SubscribePrice 中选择数据 (select2) ] 而我们选择的内容仅存储在 table SubscribeToPromoCode.

目前,当我尝试传递属性时,我得到 NULL

我在所有 table 中有很多连接

型号: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,
        ]);
    }
}

view

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

结果:错误:在 null 上调用成员函数 isAttributeRequired()。

我做错了什么?

日志:

Error: Call to a member function isAttributeRequired() on null in C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\widgets\ActiveField.php:859
Stack trace:
#0 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\widgets\ActiveField.php(720): yii\widgets\ActiveField->addAriaAttributes(Array)
#1 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\backend\views\promo-code\_form.php(36): yii\widgets\ActiveField->widget('kartik\widgets\...', Array)
 #2 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\View.php(328): require('C:\program1\Ope...')
#3 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\View.php(250): yii\base\View->renderPhpFile('C:\program1\Ope...', Array)
#4 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\View.php(152): yii\base\View->renderFile('C:\program1\Ope...', Array, NULL)
#5 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\backend\views\promo-code\create.php(18): yii\base\View->render('_form', Array)
#6 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\View.php(328): require('C:\program1\Ope...')
#7 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\View.php(250): yii\base\View->renderPhpFile('C:\program1\Ope...', Array)
#8 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\View.php(152): yii\base\View->renderFile('C:\program1\Ope...', Array, Object(backend\controllers\PromoCodeController))
#9 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\Controller.php(381): yii\base\View->render('create', Array, Object(backend\controllers\PromoCodeController))
#10 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\backend\controllers\PromoCodeController.php(82): yii\base\Controller->render('create', Array)
#11 [internal function]: backend\controllers\PromoCodeController->actionCreate()
#12 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#13 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#14 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\Module.php(523): yii\base\Controller->runAction('create', Array)
#15 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\web\Application.php(102): yii\base\Module->runAction('promo-code/crea...', Array)
#16 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#17 C:\program1\OpenServer\OSPanel\domains\masterRoom\master\backend\web\index.php(19): yii\base\Application->run()
#18 {main}

更新

除了之前发现的问题之外,您在 Select2

data 选项中也有同样的错误
'data' => SubscribePrice::find()->orderBy('currency'),

你也在那里传递 ActiveRecord 实例,而你需要传递一个带有 name=>value 对的数组,它应该是

'data' => \yii\helpers\ArrayHelper::map(SubscribePrice::find()->orderBy('currency')->all(),'id','currency')

使用 ArrayHelper::map() 将列提取为关联数组并将它们列在 select2 中。


你的问题在actionCreate()

的第二行
$price = SubscribePrice::find();

因为 $price 持有 ActiveRecord 实例而不是 model 实例并且 $price 在这一行中作为 promo 传递给视图

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

进一步用于 ActiveForm 字段填充 <?= $form->field($promo, 'description')->widget(Select2::className(), [,而填充字段时您需要模型实例而不是 ActiveQuery,因此将其更改为。

$price = new SubscribePrice();

$price = SubscribePrice::find()->where(['column'=>$value])->limit(1)->one();

满足您的要求。