Welcome Guest, Not a member yet? Register   Sign In
Active Record like testing built into a function.
#1

[eluser]Unknown[/eluser]
I apologize if this is more of a general php question but I really don't know where to start on this task.

I have MY_Model setup with a function that takes incoming data, cleanses it, and runs an INSERT or UPDATE depending on the data. Like this...
Code:
public function converter($xdata, $table = false, &$form = false, $message = false) {
  $data = array();
  foreach($xdata as $key => $value) {
   $keys = substr($key, 1);
   $data[$keys] = $value;
   if($data[$keys] == 'CheckPassword') {
    // CHECK Password
    unset($data[$keys]);
    // $data
   }
   if(is_array($data[$keys])) {
    //For Select list and radio groups and checkboxes.
    if(!empty($data[$keys][1])) {
      // Check for multi select list.
    } else {
     $data[$keys] = $data[$keys][0];
     if($data[$keys] == '') {
      unset($data[$keys]);
     } elseif($data[$keys] === '-') {
      $data[$keys] = NULL;
     }
    }
   } elseif(preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/', $value)) {
    $data[$keys] = date('Y-m-d', strtotime($value));
   } elseif($value == '') {
    // FOR empty values
    $data[$keys] = NULL;
   }
  }
  if(isset($data['submit'])) unset($data['submit']);
  if(isset($data['Submit'])) unset($data['Submit']);
  if($table == TRUE) {
   $result = mysql_query("SHOW COLUMNS FROM `".$table."` LIKE 'Author'");
   $exists = (mysql_num_rows($result));
   if(!empty($exists)) $data['Author'] = $this->info->ID;
   $result2 = mysql_query("SHOW COLUMNS FROM `".$table."` LIKE 'dateadded'");
   $exists2 = (mysql_num_rows($result2));
   if((!empty($exists2)) && (!isset($data['ID']))) $data['DateAdded'] = date("Y-m-d");
   if(isset($data['ID'])) {
    $this->db->where('id',$data['ID']);
    $this->db->update($table, $data);
    $data['NEW'] = FALSE;
   } else {
    $this->db->insert($table, $data);
    $data['ID'] = $this->db->insert_id();
    $data['NEW'] = TRUE;
   }
   $error = $this->db->_error_message();
   if (!empty($error) && !empty($form)) {
    $form->add_error($error);
   } else {
    $this->session->set_flashdata('mtitle', 'Success!');
    if($data['NEW']) $this->session->set_flashdata('message', $message.' has been created.');
    else $this->session->set_flashdata('message', $message.' has been updated.');
   }
  }
  return $data;
  }
What I want to add is the ability to have custom tests and data manipulation happen basically in the middle of the function (after the array manipulation but before the INSERT/UPDATE statements) So I say like active record because I'd like to create the tests and then call the function as normal for example
Code:
$this->custom_manipulation->( if($data['Completed'] == 'Yes') $data['DateCompleted'] = date('Y-m-d') );
$data = $this->converter($post_data,$tblPurchaseItems, $form,'Purchase Item');
Let me know if this an idiotic idea or I'm barking up the wrong tree. Any creative alternatives are welcome. Thanks you all so much for a wonderful product!




Theme © iAndrew 2016 - Forum software by © MyBB