• 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transaction

#1
HI sorry for english, in my controller i control if a record is a user's record before delete the record   :

PHP Code:
  function delete_device($id_record){

    //controllo device di utente
    if(!$this->device_model->control_record_of_user($id_record,$_SESSION['user_id'])){

      $data['messaggi_errore'] = lang('record_non_di_user');

      $data ['redirect'] =base_url().'index.php/device/list_device';

      $this->load->view('templates/header_client',$data );
      $this->load->view('templates/menu_client',$data );

      $this->load->view('common/messaggi_errore',$data );

      $this->load->view('templates/footer_client' ,$data);



    }else{

      $res=$this->device_model
                
->deleteDevice_plus_rimuovi_dispositivi($id_record);


     if($res){

      $data['messaggi_ok'] = lang('inv_record_eliminato');

      $data ['redirect'] =base_url().'index.php/device/list_device';

               $this->load->view('templates/header_client' ,$data);
               $this->load->view('templates/menu_client' ,$data);
               $this->load->view('common/messaggi_ok_no_time'$data);
               $this->load->view('templates/footer_client' ,$data);

      }else{

        $data['messaggi_errore'] = lang('inv_problemi_eliminazione_record');

        $data ['redirect'] =base_url().'index.php/device/list_device';

        $this->load->view('templates/header_client',$data );
        $this->load->view('templates/menu_client',$data );

        $this->load->view('common/messaggi_errore',$data );

        $this->load->view('templates/footer_client' ,$data);


      }

    }

  

this is the fuction to control if record is is a user's record :

PHP Code:
  function control_record_of_user($id_record,$user_id){


    $device $this->get_Device($id_record) ;//get record


    if($device->id_user==$user_id){

      return TRUE;

    }else{

      return FALSE;

    }





  



in my model i have a transaction  in deleteDevice_plus_rimuovi_dispositivi:

PHP Code:
function deleteDevice_plus_rimuovi_dispositivi($record){

      $this->load->model('dev_type_a_model');
      $this->load->model('dev_type_b_model');
      $this->load->model('dev_type_c_model');

      $this->db->trans_start();

      //RIMUOVO I type_a
      $this->dev_type_a_model->rimuovi_type_a_in_uso($record);
      
      
//RIMUOVO I type_b 
      $this->dev_type_b_model->rimuovi_type_b_in_uso($record);

      //RIMUOVO I type_c 
      $this->dev_type_c_model->rimuovi_type_c_in_uso($record);
      
      
//cancello il device
      $this->deleteDevice($record);



      $this->db->trans_complete();

      if ($this->db->trans_status() !== FALSE)
      {
        return TRUE;

      }else{

        return FALSE;

      }

    


With transaction i have an error as the controller control many time if the record is a user's record :
PHP Code:
<h4>A PHP Error was encountered</h4>

<
p>SeverityNotice</p>
<
p>Message:  Trying to get property of non-object</p>
<
p>Filenamemodels/Device_model.php</p>
<
p>Line Number658</p

This is line number 658     if($device->id_user==$user_id){

The process work fine (but i see the error  for  half of second)


if i delete only the record without transaction
 ( deleteDevice($record )   )
 
i haven't error . Why ?
Reply

#2
Error is not CI related, it is a general PHP error.


Code:
function control_record_of_user($id_record,$user_id){

    $device = $this->get_Device($id_record) ;//get record
    
    //here run print_r($device); die(); to check the what is inside of the $device variable.
    // you can update codes below like this: return $device->id_user==$user_id;
    if($device->id_user==$user_id){

      return TRUE;

    }else{

      return FALSE;

    }

  }

In your method you are not checking if the get_Device method returning an object, if there is not record with that $id_record it might return NULL. Or as error says even if it returns something it is type maybe not an object but array. By running print_r($device) check what is inside of it.
Also, I suggest to not run separate method to check if the record belongs to the user. Instead, build your SQL query with the condition for example:

DELETE from records
WHERE record_id = 2 and id_user = 56

Also, you can build DB constraints (Foreign key, DB relations).
For example, you have users and users have records. In DB you can build a constraint saying if a user is deleted then delete its records also.
This way you don't have to run separate queries, and use transactions.
Reply

#3
(04-26-2020, 09:52 AM)neuron Wrote: Error is not CI related, it is a general PHP error.


Code:
function control_record_of_user($id_record,$user_id){

    $device = $this->get_Device($id_record) ;//get record
   
    //here run print_r($device); die(); to check the what is inside of the $device variable.
    // you can update codes below like this: return $device->id_user==$user_id;
    if($device->id_user==$user_id){

      return TRUE;

    }else{

      return FALSE;

    }

  }

In your method you are not checking if the get_Device method returning an object, if there is not record with that $id_record it might return NULL. Or as error says even if it returns something it is type maybe not an object but array. By running print_r($device) check what is inside of it.
Also, I suggest to not run separate method to check if the record belongs to the user. Instead, build your SQL query with the condition for example:

DELETE from records
WHERE record_id = 2 and id_user = 56

Also, you can build DB constraints (Foreign key, DB relations).
For example, you have users and users have records. In DB you can build a constraint saying if a user is deleted then delete its records also.
This way you don't have to run separate queries, and use transactions.


The method :
Code:
    public function get_Device($id_record){

        $this->db->where('id', $id_record);


           $q=$this->db->get('device');

          $res=$q->row();

          return $res;



    }

return object because to delete must push on a button and with jquery i get  the id to delete .
With only deleteDevice($record) (in the model inside a transaction) called in place of deleteDevice_plus_rimuovi_dispositivi($record) not error  are showing .

F
or this I suggest to not run separate method to check if the record belongs to the user. Instead, build your SQL query with the condition for example

I do it to show the right mistake.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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