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

Null Records on save?

Hi... I'm trying to get familiar with the framework by working through the tutorials. Got everything compiled and installed and started working through the first example. Everything works fine up until the save record. Save doesn't seem to be working correctly and I can't figure out why..

tutorial I've followed to the letter: https://docs.phalcon.io/en/latest/reference/tutorial.html#checking-your-installation

In the last step, I enter some data in the two text fields and get the following: Sorry, the following problems were generated: name is required email is required

So I added an echo print_r($_POST); in the controller/registerAction and end up with this output: Array ( [name] => test [email] => test ) 1Sorry, the following problems were generated: name is required email is required

I removed the "not null" requirement and it saves an empty record... regardless of what is in the $_POST array Array ( [name] => test [email] => test ) 1Thanks for register!

Any idea on how I can start tracking down the issue?



98.9k
Accepted
answer

In 1.0.x, the mass assignment was disabled due to a security issue, you can directly assign the values to the model:

$user = new Users();
$user->name = $this->request->getPost("name");
$user->email = $this->request->getPost("email");
$user->save();

In 1.1.0 this issue was fixed.

worked great... thank you! I'll work on pulling in 1.1.0 too... just pulled the master branch to get started. Thx!

edited Oct '14

Hi, I'm having the same problem as @ceallred. I'm using Phalcon 1.1.0, and following the same tutorial. I tried manual assignment as suggested, however this did not help, it still considered the "email" field to be blank. If i allow the table fields to be blank, however, it will save fine.

Controller function:

public function registerAction(){

    $user = new Users();

    $user->name = $this->request->getPost('name');
    $user->email = $this->request->getPost('email');

    $user->save();
  }

View:

<?php echo Tag::form("signup/register"); ?>

 <p>
    <label for="name">Name</label>
    <?php echo Tag::textField("name") ?>
 </p>

 <p>
    <label for="name">E-Mail</label>
    <?php echo Tag::textField("email") ?>
 </p>

 <p>
    <?php echo Tag::submitButton("Register") ?>
 </p>

</form>

Model:

<?php

class Users extends \Phalcon\Mvc\Model{

}
?>


98.9k

Hi Edward, if you want compare the code with the one in this repository, maybe there is a typo somewhere there,

https://github.com/phalcon/tutorial