I'm trying to learn yii 2.0 by creating a simple form for adding new posts.
Here's the respective method in my SiteController (have also added use app\models\Posts; at the top):
public function actionSave($id=NULL){
if($id = NULL)
$model = new Posts;
else
$model = $this->loadModel($id);
if(isset($_POST['Posts'])){
$model->load($_POST);
if($model->save()){
Yii::$app->session->setFlash('success', 'Model has been saved');
$this->redirect($this->createUrl('site/save', ['id' => $model->id]));
}else
Yii::$app->session->setFlash('error', 'Model could not be saved');
}
echo $this->render('save', ['model' => $model]);
}
It renders save view file. Here's that view file:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['options' => ['class' => 'form-horizontal', 'role' => 'form']]) ?>
<div class="form-group">
<?php echo $form->field($model, 'title')->textInput(['class' => 'form-control']); ?>
</div>
<div class="form-group">
<?php echo $form->field($model, 'data')->textArea(['class' => 'form-control']); ?>
</div>
<?php echo Html::submitButton('Submit', ['class' => 'btn btn-primary pull-right']); ?>
<?php ActiveForm::end();
I'm expecting a form, but it's showing an error Calling unknown method: yii\db\ActiveQuery::formName()
What am i doing wrong here?
$form->field()from view