0

I am having an issue inserting a record into the database. I am a beginner with the Yii framework, so I may have made some stupid mistakes.

This is from the SiteController

public function actionCreatePost(){
     $model = new PostForm();

     $post = new Post();

    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $post->body = $model->body;
        $post->title = $model->title;
        $post->save();

        return $this->redirect('index');
    }else {
        return $this->render('createPost', ['model' => $model]);
    }
}

This is from the Post class

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at',
            'updatedAtAttribute' => 'updated_at',
            'value' => new Expression('NOW()'),
        ],
        [
            'class' => BlameableBehavior::className(),
            'createdByAttribute' => 'id_author',
        ]
    ];
}
Alex Csillag
  • 41
  • 1
  • 5

2 Answers2

1

The issue is that you have created a PostForm class for the form (which is correct) but you are then trying to load the response into the Post class - a completely different class. This won’t work without modification.

If you have a look at the response:

var_dump(Yii:$app->request->post()); 

You will see the form data is located within the PostForm key. Yii will therefore only load the data into the PostForm class.

The correct solution is therefore to create a savePost() function within the PostForm eg:

public function savePost(){
$model = new Post();
$model->propertyABC = $this->propertyABC
...etc...
$model->save();

So the action would appear as follows:

$model = new PostForm();
If($model->load(Yii::$app->request->post()) && $model->validate()){
$model->savePost();

The other option is to rename the key from PostForm to Post. Yii will then load the data but this is not the best approach as it is a bit obscure.

Hope that helps

DrBorrow
  • 809
  • 8
  • 19
0

I would guess the issue is with the validation.

I can see several issues I will point out. First, I cannot figure out why are you creating a new PostForm, loading the data in it and verifying it, just to dump some values in a new Post and save it. Are there some functions, you are running in the PostForm model, that are triggered by load or verify? If that is not the case, I would suggest dropping one of the models, and using only the other. Usually, that is the Form model. It serves as a link between the ActiveForm and the model handling everything. You can do everything in the createPost() function in the Form model, and then in the controller it will look like

    if ($model->load(Yii::$app->request->post())) {
          $model->save();
          return $this->redirect('index');
    }

Second of all, you can dump post->getErrors() before the save to see if there are any errors with the validation. What you can also do, is call $post->save(false) instead. If you pass false to it, it will not trigger $post->validate(), and some errors can be neglected. Please, let me know if there is anything unclear.

Martin Dimitrov
  • 1,319
  • 1
  • 9
  • 23