0
votes

When I try to display data from a related table, I get an error Call to a member function isAttributeRequired() on null.

I have tables: PromoCode, SubscribePrice and PromoToSubscribePrice. But when I try to display the table Sub in the promo code I get an error.

I need the following link: When creating a table PromoCode, we could make a selection of data (select2) from a table SubscribePrice and what we chose there was stored only in the table SubscribeToPromoCode.

Currently, when I try to pass an attribute, I get NULL

I have many to many connections in all tables

Model: PromoCode

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

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

and view:

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

Result: Error: Call to a member function isAttributeRequired() on null.

What am I doing wrong?

Log:

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}
1

1 Answers

1
votes

Update

Apart from the problem identified earlier you have the same mistake in the data option of the Select2

'data' => SubscribePrice::find()->orderBy('currency'),

you are passing the ActiveRecord instance there too whereas you need to pass an array with name=>value pairs it should be

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

Use ArrayHelper::map() for extracting the columns as associative array and listing them inside the select2.


Your problem is here in the second line of actionCreate()

$price = SubscribePrice::find();

as the $price is holding the ActiveRecord instance and not the model instance and the $price is passed to the view as promo in this line

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

which is further used in the ActiveForm field population <?= $form->field($promo, 'description')->widget(Select2::className(), [, whereas while populating the field you need a model instance and not the ActiveQuery so change it to.

$price = new SubscribePrice();

or

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

whichever fulfills your requirements.