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

Inserting specific form data to database

Hello.

I have a set of form elements like this snippet below, but i only want to insert the name and address value into database, the rest is for validation only. How can i do that ? I tried using save() function like in the tutorial but it didn't work (wrong parameters exception). In my User database, there's no password column, only name and address

The form:

$name = new Text("name");
$name->setLabel("Name");
$name->setFilters(array('striptags', 'string'));
$name->addValidators(array(
  new PresenceOf(array(
  'message' => 'Name is required'
  ))
));
$this->add($name);

$pass = new Password("password");
$pass->setLabel("Password");
$pass->addValidators(array(
  new PresenceOf(array(
  'message' => 'Password is required'
  )),

  new Confirmation(array(
  'message' => 'Password does not match',
  'with' => 'cPass'
  ))
));
$this->add($pass);

$address = new TextArea("address");
$address->setLabel("Address");
$address->setFilters(array('striptags', 'string'));
$address->addValidators(array(
  new PresenceOf(array(
  'message' => 'Address is required'
  ))
));
$this->add($address);

The controller:

public function doRegisterAction(){

  if (!$this->request->isPost()) {
     return $this->forward("index/index");
  }

  $form = new register;
  $user = new user();

  $data = $this->request->getPost();
  if (!$form->isValid($data, $user)) {
    $error = array();
    foreach ($form->getMessages() as $message) {
        array_push($error, $message);
    }
  $this->view->setVar('error', $error);
  return $this->forward('register/index');
  }

  if ($user->save() == false) {
    foreach ($form->getMessages() as $message) {
        echo $message;
    }
  return $this->forward('register/index');
  }
  $form->clear();

  echo "User registered successfully";
  return $this->forward("index/index");
  }

Thanks in advance :)



43.9k

Hi,

does this not come from your user model? Don't you have more attributes then just name and address ?



6.6k

Hi,

does this not come from your user model? Don't you have more attributes then just name and address ?

Well yes, both my model and form have more attributes in it, i just cut it because it will just be a too long of a snippet.

In my model i have this :


class user extends Model{
    public $userID;
    public $name;
    public $email;
    public $phone;
    public $city;
    public $address;
    public $postal;
    public $joinDate;
    public $balance;
    public $accStatus;

    public function initialize(){
        //Bunch of code
    }   

And in my form i have this columns :

Name, Password, Confirm Password, Address, Email, Phone, City, and Postal



43.9k
Accepted
answer

phalcon's can't save a new user record if not all attributes are setted.

If you really want to create uncomplete objects you can use column skipping.

For automatic task on Model creation/update(created_at, updated_at, user_id...) , you can use behaviors or events manager

Hope this helps



6.6k

phalcon's can't save a new user record if not all attributes are setted.

If you really want to create uncomplete objects you can use column skipping.

For automatic task on Model creation/update(created_at, updated_at, user_id...) , you can use behaviors or events manager

Hope this helps

Ah, i forgot about column skipping thing. Thank you so much :D