Lock
Lock

Reputation: 5522

Update Phalcon model from POST values

Is there a way in Phalcon to pass a posted form to a model and have it updates (or create) the models values?

Such as, if I have a model such as:

<?php
class User extends ModelBase
{
  public $id;
  public $first_name;
  public $last_name;
  public $email;
  public $username;
  public $password;
  public $active;      

Is there a way to pass the posted array (providing the field names are the same) and have it update the model after calling save()?

Upvotes: 0

Views: 4472

Answers (1)

jodator
jodator

Reputation: 2465

Sure you can:

$user = Users::find(1);
$user->update($this->request->getPost());
// or
$user->update($this->request->getPost(), array('first_name', 'last_name'));

You can also use $user->save() to create/update model if needed. As far as I remember Phalcon will update or create user model depending if primary_key of that model exists in database.

More here: request env and updating models.

Upvotes: 2

Related Questions