Welcome Guest, Not a member yet? Register   Sign In
Problem with edit data in db with form
#4

If I understand what you're trying to do, you could do something like this in your base model:

PHP Code:
<?php

class MY_Model extends CI_Model
{
 
   /** @var string The name of the primary db table used by this model. */
 
   protected $tableName '';

 
   /** @var string|array The primary key of the table. */
 
   protected $key 'id';
 
   
    
// ...

 
   /**
     * Metadata for the model's database fields.
     * @var array
     *
     * This could also be hard-coded to avoid a database call when using 
     * $this->prepData() and/or $this->getFieldInfo().
     *
     * @see http://www.codeigniter.com/user_guide/database/metadata.html#retrieve-field-metadata
     *
     * Each field definition should be an array containing the following keys:
     * 'name' => the name of the field in the database table
     * 'type' => the data type of the field
     * 'default' => (optional) the default value
     * 'max_length' => (optional) the maximum length of the value
     * 'primary_key' => (optional) 1 if the column is a primary key
     */
 
   protected $fieldInfo = array();

 
   public function __construct()
 
   {
 
       parent::__construct();

 
       // ...

 
       if (! empty($this->fieldInfo)) {
 
           foreach ($this->fieldInfo as $key => $field) {
 
               $this->fieldInfo[$key] = (object) $field;
 
           }
 
       }

 
       // ...
 
   }

 
   /**
     * Get the metadata for the model's database fields.
     * 
     * Retrieves the metadata from the database if $this->fieldInfo is empty.
     *
     * @return array The database field metadata for this model.
     */
 
   public function getFieldInfo()
 
   {
 
       if (empty($this->fieldInfo)) {
 
           $this->fieldInfo $this->db->field_data($this->tableName);
 
       }

 
       return $this->fieldInfo;
 
   }

 
   public function prepData($postData)
 
   {
 
       // Populate $skippedFields with any fields which may be auto-populated by
 
       // your model, like created/modified/soft-delete fields.
 
       $skippedFields = array();
 
       // for example:
 
       $skippedFields[] = $this->get_created_field();
 
       $skippedFields[] = $this->get_created_by_field();
 
       $skippedFields[] = $this->get_deleted_field();
 
       $skippedFields[] = $this->get_deleted_by_field();
 
       $skippedFields[] = $this->get_modified_field();
 
       $skippedFields[] = $this->get_modified_by_field();

 
       // $this->key could be an array or a string...
 
       $skippedFields array_merge($skippedFields, (array) $this->key);

 
       $data = array();

 
       // Loop through the table's fields.
 
       foreach ($this->getFieldInfo() as $field) {
 
           // If the field is the primary key, one of the skipped fields, or has 
 
           // not been set in $postData, skip it.
 
           if (isset($field->primary_key) && $field->primary_key
                
|| in_array($field->name$skippedFields)
 
               || ! isset($postData[$field->name])
 
           ) {
 
               continue;
 
           }

 
           // Add the field to the returned $data.
 
           $data[$field->name] = $postData[$field->name];
 
       }

 
       return $data;
 
   }


Then, in your individual models, you would set the properties accordingly, and override the prepData() method (calling the parent's prepData() method) to add any additional prep you need to perform on specific fields. The primary purpose of this is that you can pass the entire output of $this->input->post() into prepData() and it will only return the fields for the table specified in the model's $tableName property (minus any fields you wish to skip).

For example, I might have some special handling in a model like this:


PHP Code:
public function prepData($postData)
{
 
   $data parent::prepData($postData);

 
   // Checkbox.
 
   $data['example_closed'] = empty($data['example_closed']) ? 1;

 
   // Fields which default to null.
 
   $data['example_due_date'      = empty($data['example_due_date']) ? null $data['example_due_date'];
 
   $data['example_billable_hours'] = empty($data['example_billable_hours']) ? null $data['example_billable_hours'];

 
   // Fields which default to empty strings.
 
   $data['example_assigned_to'] = empty($data['example_assigned_to']) ? '' $data['example_assigned_to'];
 
   $data['example_notes'      = empty($data['example_notes']) ? '' $data['example_notes'];

 
   return $data;


Then my controller might do something like this:


PHP Code:
// Assuming the model has a getValidationRules() method which returns an array 
// compatible with the form_validation library's set_rules() method.
$this->form_validation->set_rules($this->example_model->getValidationRules());
if (
$this->form_validation->run() === false) {
 
   return false;
}

// Only pass in the desired fields.
$data $this->example_model->prep_data($this->input->post());

// If the controller needs to do something special with the data, do it here...

// Send the data back to the model for insert/update in the database. 
Reply


Messages In This Thread
RE: Problem with edit data in db with form - by mwhitney - 06-10-2015, 07:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB