When creating a plugin with Craft CMS one often need to save a record into database, with data coming from a form.
The problem I faced was that the record was not saved, even if it validated and the save function return true.
Looking into mysql queries I saw that the query was an update one and not an insert, let’s see some code first :
Take this controller action for exemple, a very basic exemple without error management for demonstration purpose
And the corresponding service, in charge of db relations :
As I said even if everything was executed without errors, the record did not show in the table.
Using the fact that the query was an update and not an insert, I looked and found that printing
would return false
As of now I do not now what cause this, but the fix is easy, just apply
And voilà, the record shows up in the table.