2016-09-06 34 views
0

我想使用我發現很好的代碼How to create a Yii2 model without a database中解釋的代碼,但它給了我未知屬性錯誤:「Gettting unknown property:應用程序... \ DBcomponents :: newhost「。 也許我使用了錯誤的Model類。任何人都可以請向我解釋是什麼問題?謝謝!未知屬性,當試圖使用未連接到數據庫的模型yii2

這裏有代碼:

的觀點:

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

形式:

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

和模型:

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', 
    ]; 
}} 
+0

將你的變量定義爲公共變量 – yafater

+0

它的工作,非常感謝你 – mada

回答

0

由於@yafater建議我將變量設置爲public它解決了這個問題。謝謝

相關問題