2

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?

8
  • $form = ActiveForm::begin(); try without any value in begin funciton Commented Oct 17, 2014 at 12:24
  • when you get error while Add or Edit? Commented Oct 17, 2014 at 12:27
  • Still doesn't work. It works if i remove those two $form->field() from view Commented Oct 17, 2014 at 12:30
  • I'm not able to load page. So can't add/edit. Commented Oct 17, 2014 at 12:31
  • Post your whole controller code here? Commented Oct 17, 2014 at 12:32

3 Answers 3

1
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');

             //Updated 
             $this->redirect(['save', 'id' =>$model->id]);

             //Remove this 
            //$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]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I suppose you use the loadModel function, if so, change this line

$model = Posts::find($id); 

to this

$model = Posts::find()->where(['id' => $id])->one();

The error occurs because the object returned by loadModel is a ActiveQuery and the ActiveForm expected a ActiveRecord.

Comments

0
Calling unknown method: frontend\models\SignupForm::save() 

SiteController.php


 public function actionSignup()
    {
        $model = new SignupForm();

         if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $fileName = $model->username;
            $model->file =UploadedFile::getInstance($model,'file');
            $model->file->saveAs('uploads/'.$fileName.'.'.$model->file->extension );
            $model->filePath = 'uploads/'.$fileName.'.'.$model->file->extension ;
            $model->save();
            return $this->redirect(['view', 'id' => $model->idQuiz]);
        //}


       // if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }


signup.php


                 <?= $form->field($model, 'file')-> fileinput(); ?>


SignupForm.php

<?php
namespace frontend\models;

use common\models\User;
use yii\base\Model;
use Yii;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $first_name;
    public $last_name;
    public $address;
    public $state_id;
    public $city_id;
    public $file;
    public $filePath;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['first_name', 'required'],
            ['last_name', 'required'],
            ['address', 'required'],
            ['state_id', 'required'],
            ['city_id', 'required'],
            ['filePath', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }

        $user = new User();
        $user->first_name = $this->first_name;
        $user->last_name = $this->last_name;
        $user->address = $this->address;
        $user->state_id = $this->state_id;
        $user->city_id = $this->city_id;
        $user->filePath = $this->filePath;
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();

        return $user->save() ? $user : null;
    }
}

1 Comment

Please elaborate more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.