mada
mada

Reputation: 31

Unknown property when trying to use a model which is not connected to a database yii2

I am trying to use the code I found very well explained in this post How to create a Yii2 model without a database , but it gives me the Unknown property error : "Gettting unknown property: app...\DBcomponents::newhost" . Maybe I am using wrong the Model class. Could anyone please explain to me what is the problem? Thank you!

Here you have the code:

The view:

div class="db-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_db', [
// the model is DBcomponents
'model' => $model,
]) ?>
</div>

The form:

<div class="db-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'newhost')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'dbname')->textInput() ?>
<?= $form->field($model, 'username')->textInput(['maxlength' => true])?>
<?= $form->field($model, 'password')->textInput() ?>
<div class="form-group">
    <?= Html::submitButton( 'Connect', ['class' =>'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

And the model:

class DBcomponents extends \yii\base\Model
{/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['username', 'password','newhost','dbname',], 'required'],
        [['username', 'password','newhost','dbname',], 'string', 'max'=> 100],
    ];
}
/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'username' => 'username',
        'password' => 'password',
        'newhost' => 'newhost',
        'dbname' => 'dbname',
    ];
}}

Upvotes: 0

Views: 88

Answers (1)

mada
mada

Reputation: 31

As @yafater suggested I set the variables public and it soleved the problem. Thank you

Upvotes: 0

Related Questions