调用未知方法:yii \ db \ ActiveQuery :: formName()

时间:2014-10-17 10:10:44

标签: php forms yii2

我尝试learn yii 2.0创建一个简单的表单来添加新的posts

以下是SiteController中的相应方法(还添加了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]);
}

呈现save视图文件。这是查看文件:

<?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();

我期待一个表单,但它显示错误Calling unknown method: yii\db\ActiveQuery::formName()

我在这里做错了什么?

3 个答案:

答案 0 :(得分: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]);
}

答案 1 :(得分:0)

我想您使用loadModel函数,如果是这样,请更改此行

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

到这个

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

发生错误是因为loadModel返回的对象是ActiveQuery而ActiveForm需要ActiveRecord。

答案 2 :(得分: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;
    }
}