Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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: http://docs.phalconphp.com/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?



85.4k
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{

}
?>


85.4k

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