Welcome Guest, Not a member yet? Register   Sign In
Revising Function
#1

[eluser]xtremer360[/eluser]
I'm trying to figure out what would be the best route in handling the situation of what would happen if the title is created but then the second insert doesn't get ran successfully. What are some possibilities?

Code:
/**
  * save_title function.
  *
  * @access public
  * @param string $title_name
  * @param string $title_directory_name
  * @param integer $title_status_id
  *
  * @return TRUE/FALSE
  * Should return title_id, title_name, title_directory_name, title_status_id
  */
public function save_title($title_name, $title_directory_name, $title_status_id)
{
  $data = array(
     'title_name' => $title_name,
     'title_directory_name' => $title_directory_name,
     'title_status_id' => $title_status_id
  );

  $this->db->insert('titles', $data);
  
  if ($this->db->affected_rows() == 1)
  {
   $data = array(
    'title_id' => $this->db->insert_id()
   );
  
   $this->db->insert('title_champions', $data);
  
   if ($this->db->affected_rows() == 1)
   {
    return true;
   }
   else
   {
    return false;
   }
  }
  
}
#2

[eluser]apodner[/eluser]
You can just basically roll the first insert back if the 2nd one fails.
Code:
public function save_title($title_name, $title_directory_name, $title_status_id)
{
    $data = array(
     'title_name' => $title_name,
     'title_directory_name' => $title_directory_name,
     'title_status_id' => $title_status_id
    );

    $this->db->insert('titles', $data);  
    if ($this->db->affected_rows() == 1)
    {
        $insert_id = $this->db->insert_id();
        $data = array(
        'title_id' => $insert_id
        );

        $this->db->insert('title_champions', $data);

        if ($this->db->affected_rows() == 1)
        {
            return true;
        }
        else
        {
            $this->db->where('title_id', $insert_id);
            $this->db->delete('titles');
            return false;
        }
    }
    else {
        
        return false;
    }
}
#3

[eluser]CroNiX[/eluser]
This is what transactions are for.




Theme © iAndrew 2016 - Forum software by © MyBB