Skip to content
Advertisement

Can’t insert into database with save()

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

This is from the Post class

Advertisement

Answer

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:

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:

So the action would appear as follows:

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

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement