model doesn't seem able to assign

so I'm following the basic tutorial. I've got the Users model created:

use Phalcon\Mvc\Model;

    class Users extends Model
    {
        public $id;
        public $name;
        public $email;
    }

I've created the registerAction method:

    public function registerAction()
    {
        $user = new Users();

        $user->assign(
          $this->request->getPost(),
          null,
          [
            "name",
            "email",
          ]
        );

        var_dump($this->request->getPost());
        echo '<br>';
        var_dump($user);

        $success = $user->save();

        if($success) {
            echo 'thank you for registering.';
        } else {
            echo 'sorry, the following problems were generated: ';

            $messages = $user->getMessages();

            foreach($messages as $message) {
                echo $message->getMessage(), '<br>';
            }
        }
        $this->view->disable();
    }

the var_dumps are giving me:

    Array(2) { ["name"]=> string(3) "me" ["email"]=> string(14) "[email protected]" }

    object(Users)#35 (18) { ["id"]=> NULL ["name"]=> NULL ["email"]=> NULL

so somewhere, that assign is borking.

Where?

Thanks.

Hi gedq, What appends if you remove the value null in the second parameter ? Like that : $user->assign( $this->request->getPost(), [ "name", "email", ] );



2.6k

It works. But the tutorial and the documentation both say to put a null in there. So... does that mean the basic tutorial is wrong and the documentation can't be trusted?

Hi gedq, What appends if you remove the value null in the second parameter ? Like that : $user->assign( $this->request->getPost(), [ "name", "email", ] );

edited 20d ago

It works. But the tutorial and the documentation both say to put a null in there. So... does that mean the basic tutorial is wrong and the documentation can't be trusted?

Hi gedq, What appends if you remove the value null in the second parameter ? Like that : $user->assign( $this->request->getPost(), [ "name", "email", ] );

Well, have you read the documentation for Model::assign? https://docs.phalcon.io/latest/en/db-models

It clearly shows the two use-cases:

// Assign by db row, column map needed
$robot->assign(
    $dbRow,
    [
        "db_type" => "type",
        "db_name" => "name",
        "db_year" => "year",
    ]
);
// Allow assign only name and year
$robot->assign(
    $_POST,
    null,
    [
        "name",
        "year",
    ]
);

The second argument is a coluimn map, the last argument is a whitelist of columns.

I've never used assign(), but it looks pretty useful - if it worked. On version 3.4, I was only get it to work in these cases:

$Test->assign($data);
// and, in another iteration
$Test->assign($data,NULL);

Theoretically, one should be able to pass the keys you want imported from $data, but I didn't find that to be the case.

In reality, I wouldn't bulk assign directly from POST anyway. In order to trust the data, you'd have to set up a bunch of validation in your model - and I imagine you haven't got to that part of the tutorial yet. At this stage, I'd recommend validating and setting each property individually:

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