• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update instead insert

#1
HI i have two model : 

User : 
protected $allowedFields = ['id','email','password','user_activation_hash','id_stato_users','pin','tipo_utente','refertatore','sconto','updated_at','last_login'];

protected $allowedFields = ['id_user','nome','cognome','data_nascita','telefono','nome_studio','numero_iscrizione_ordine','anno_iscrizione_ordine','piva','id_comuni','indirizzo','civico','cap','pec','sdi','note','normativa_ministero_salute','termini','privacy','comunicazioni'];


this is a part of controller's method :

Code:
if($this->validate($rules)){

              $post = $this->request->getPost();

              // generate random hash for email verification (40 char string)
              $user_activation_hash = sha1(uniqid(mt_rand(), true));

              $post['user_activation_hash'] = $user_activation_hash;

              //setto lo stato come 1 (da cliccare sul link)
              $post['id_stato_users'] = 1;

              //setto tipo utente come dentista(1)
              $post['tipo_utente'] = 1;

              $rand = rand(0, 999999);

              $post['pin'] = str_pad($rand, 6, '0', STR_PAD_LEFT);

              $res=$user_model->save($post);

              $post['id_user']=$user_model->insertID;

              log_message('debug',"post_id_user " . $post['id_user']);

              $res2=$dentistiModel->save($post);

              log_message('debug','dentistiModel last query ' . $dentistiModel->getLastQuery() );


              //DEBUG
              //$last_query = $model->getLastQuery();


            if ($res AND $res2) {


              if($this->send_user_activation_hash_by_email($post['email'],$post['pin'],$user_activation_hash))
              {

                $data['messaggi_ok'] = 'Registrazione andata a buon fine conferma il tuo indirizzo email cliccando sul link che ti abbiamo inviato';

                $data ['redirect'] =base_url().'/user/login';

                echo view('common/messaggi_ok',$data );

              }else{

                 $data['messaggio_errore'] = 'Problemi invio mail con link ';

                 $data ['redirect'] =base_url().'/user/login';

                 echo view('common/messaggi_errore',$data );

              }


            }else {

                 $data['messaggio_errore'] = 'Problemi registrazione dentista';

                 $data ['redirect'] =base_url().'/user/login';

                 echo view('common/messaggi_errore',$data );

            }

this is the log :
Code:
INFO - 2021-01-28 19:26:52 --> CSRF cookie sent
INFO - 2021-01-28 19:26:52 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2021-01-28 19:27:30 --> CSRF cookie sent
INFO - 2021-01-28 19:27:30 --> CSRF token verified
INFO - 2021-01-28 19:27:30 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
DEBUG - 2021-01-28 19:27:30 --> post_id_user 25
DEBUG - 2021-01-28 19:27:30 --> dentistiModel last query UPDATE `dentisti` SET `nome` = 'Stefano', `cognome` = 'Banchelli', `numero_iscrizione_ordine` = '212212', `anno_iscrizione_ordine` = '2025', `note` = '64656465', `normativa_ministero_salute` = '1', `termini` = '1', `privacy` = '1', `comunicazioni` = '1', `id_user` = 25
WHERE `dentisti`.`id_user` IN (25)
INFO - 2021-01-28 19:27:34 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2021-01-28 19:27:34 --> CSRF cookie sent

why don't  insert but update ?
Reply

#2
Hi
I dont kknows why, but you can read : https://codeigniter.com/user_guide/model...light=save

Its about $id
Reply

#3
(01-28-2021, 01:32 PM)neoneeco Wrote: Hi
I dont kknows why, but you can read : https://codeigniter.com/user_guide/model...light=save

Its about $id
change to insert instead  save , the record are inserted but return false , why ?

can i use transaction on model->save() and model->insert?
Reply

#4
If the $id is in the database table it will do an update, if not it will do an insert.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(01-28-2021, 09:53 PM)InsiteFX Wrote: If the $id is in the database table it will do an update, if not it will do an insert.
In the second table i havent field id
Reply

#6
I have tried to organize your coding a bit and asked you a question within the code area below. Could you please check?


PHP Code:
if($this->validate($rules)){

              //$post = $this->request->getPost();

              // generate random hash for email verification (40 char string)
              $user_activation_hash sha1(uniqid(mt_rand(), true));

              $user_activation_hash $this->$this->request->getVar('user_activation_hash');
              $id_stato_users 1;
              $tipo_utente 1;
              $rand rand(0999999);
              $pin str_pad($rand6'0'STR_PAD_LEFT);

              $data = [
                 'user_activation_hash' => $user_activation_hash,
                 'id_stato_users' => $id_stato_users,
                 'tipo_utente' => $tipo_utente,
                 'pin' => $pin
               
];
              
               $res
=$this->UserModel->save($data); // rename your model please. Name should be capital

                $id_user=$this->UserModel->getInsertID

              log_message('debug',"post_id_user " $id_user);

              $res2=$this->DentistiModel->save($data); // Why saving the same data for a different model? 
Reply

#7
(01-29-2021, 04:50 AM)demyr Wrote: I have tried to organize your coding a bit and asked you a question within the code area below. Could you please check?


PHP Code:
if($this->validate($rules)){

              //$post = $this->request->getPost();

              // generate random hash for email verification (40 char string)
              $user_activation_hash sha1(uniqid(mt_rand(), true));

              $user_activation_hash $this->$this->request->getVar('user_activation_hash');
              $id_stato_users 1;
              $tipo_utente 1;
              $rand rand(0999999);
              $pin str_pad($rand6'0'STR_PAD_LEFT);

              $data = [
                 'user_activation_hash' => $user_activation_hash,
                 'id_stato_users' => $id_stato_users,
                 'tipo_utente' => $tipo_utente,
                 'pin' => $pin
               
];
              
               $res
=$this->UserModel->save($data); // rename your model please. Name should be capital

                $id_user=$this->UserModel->getInsertID

              log_message('debug',"post_id_user " $id_user);

              $res2=$this->DentistiModel->save($data); // Why saving the same data for a different model? 


i save data for different model because i have a two type of user , the first part (a simple registration data) i save into a table users , the second part is specific of the type of user and i save it in the specific table
Reply

#8
(01-29-2021, 05:05 AM)pippuccio76 Wrote: i save data for different model because i have a two type of user , the first part (a simple registration data) i save into a table users , the second part is specific of the type of user and i save it in the specific table

I asked because I wanted to be sure if you have the same columns in your second table?

Btw, did you set auto increment for ID in your db table?
Reply

#9
(01-29-2021, 05:19 AM)demyr Wrote:
(01-29-2021, 05:05 AM)pippuccio76 Wrote: i save data for different model because i have a two type of user , the first part (a simple registration data) i save into a table users , the second part is specific of the type of user and i save it in the specific table

I asked because I wanted to be sure if you have the same columns in your second table?

Btw, did you set auto increment for ID in your db table?

I have id in user autoincrement , in dentisti i have id_user (foreign key of user ) without id
Reply

#10
Would you try insert instead of save ?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.