We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

\Phalcon\Mvc\Model->save($_POST, ...) not working. Bug?

I'm following the first tutorial and I'm having problems with the save() method. Here is the code:

//Store and check for errors
        if ($user->save($_POST, array('name', 'email')) == true) {
            echo "Thanks for register!";
        } else {
            echo "Sorry, the following problems were generated: ";
            foreach ($user->getMessages() as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }

It doesn't work, it always present this error message:

Sorry, the following problems were generated: name is required email is required

But if I set the variables like this it works:

//Store and check for errors
$user->name = $_POST['name'];
$user->email = $_POST['email'];

        if ($user->save() == true) {
            echo "Thanks for register!";
        } else {
            echo "Sorry, the following problems were generated: ";
            foreach ($user->getMessages() as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }

Any idea why this is happening? Is this a bug?



98.9k
Accepted
answer

It seems, we need to update the tutorial, the mass assignment is going to be removed due to potential possible security issues



32.2k

That's what I thought. Also the API docs need to be updated.