php - Want to insert into two tables using one form in ZF2 -


this might bit strange, i'll try explain. have table containing users , table containing customers. users have permission data of customers. figured make separate table user permissions, taking user id , customer id foreign keys , having 1 row per customer/user permission.

when admin adds new user database using class called userform (posted shortened below reference), uses zend\form, want display customers next form buttons can selected added permissions. now, thought having javascript array appends or removes customer ids if they're selected/deselected, passing array form hidden value , looping through array inserting row permissions table each customer id in array, , taking user id that's been created well. i'm not sure if best way it, it's best come with.

hope that's @ least understandable. so, have 1 form, want insert 2 different tables. question, guess, how pass array form value? , how insert not users table, permissions table, when saveuser() method called (i'll post below too). also, weird way of doing i'm being unnecessarily difficult about? i'd love hear if there's easier way.

my userform class:

namespace admin\form; use zend\form\form;  class userform extends form { public function __construct($name = null) {     parent::__construct('user');     $this->setattribute('method', 'post');     $this->add(array(         'name' => 'userid',         'attributes' => array(             'type'  => 'hidden',         ),     ));      $this->add(array(         'name' => 'activated',         'attributes' => array(             'value' => 1,             'type'  => 'hidden',         ),     ));     $this->add(array(         'name' => 'username',         'attributes' => array(             'type'  => 'text',         ),         'options' => array(             'label' => 'username:',         ),     ));     $this->add(array(         'name' => 'firstname',         'attributes' => array(             'type'  => 'text',         ),         'options' => array(             'label' => 'first name:',         ),     ));     $this->add(array(         'name' => 'lastname',         'attributes' => array(             'type'  => 'text',         ),         'options' => array(             'label' => 'last name:',         ),     ));     $this->add(array(         'name' => 'submit',         'attributes' => array(             'type'  => 'submit',             'value' => 'go',             'id' => 'submitbutton',         ),     ));  } 

}

my saveuser() method

public function saveuser(user $user) {     $data = array(         'firstname'     => $user->firstname,         'lastname'      => $user->lastname,         'username'      => $user->username,         'activated'     => $user->activated,     );      $userid = (int)$user->userid;     if ($userid == 0) {         $this->tablegateway->insert($data);     } else {         if ($this->getuser($userid)) {             $this->tablegateway->update($data, array('userid' => $userid));         } else {             throw new \exception('user id not exist');         }     } } 

one method use form fieldsets. form can have multiple fieldsets, , each fieldset can bound different entity.

these useful when have one one relationship between entities

you create teo entities, example user entity (as have) , new entity represent permission.

you create feildset each , bind objects fields sets. (userfieldset , permissionfieldset example)

checkout section on form fieldsets:

http://zf2.readthedocs.org/en/latest/modules/zend.form.advanced-use-of-forms.html

if have one many relationship, ie. 1 user can have many permissions, better off looking @ form collections:

http://zf2.readthedocs.org/en/latest/modules/zend.form.collections.html

there's example of dynamically adding new rows 1 many relationship too.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -