Welcome Guest, Not a member yet? Register   Sign In
How to map upload.php and my table information
#1

[eluser]jaswinder_rana[/eluser]
I am using upload.php library class to upload files. it's working. Now I want to insert the data returned from upload class into a table.

Data returned is
Code:
* file_name:
    * file_type:
    * file_path:
    * full_path:
    * raw_name:
    * orig_name:
    * file_ext:
    * file_size:
    * is_image:
    * image_width:
    * image_height:
    * image_type:
    * image_size_str:

My columns do NOT have the same name as the fields returned. I can change it but I was wondering (in case) I was not able to change it then how can I pass data to my model?

- Should I create an array with same index as column names, give it values and then pass it to model

- Should I pass above data and then create new array in model?

- I can also (as mentioned) change my column names and then just pass above array and have it store the information (Assuming all sanitizing/cleaning (which I am still not sure about)) is done by db class

EDIT: If I do rename my columns then how will I manage column types (some are int and some varchar)? Do I have to make all columns varchar?



Thanks
#2

[eluser]iainco[/eluser]
The way I would think about this is, try to make the controller do most of the work - leave the model to pretty much only call DB queries.

Your first idea - "Should I create an array with same index as column names, give it values and then pass it to model" seems to work for me".

If I'm reading it right you could do this:

Controllor:
Code:
function saveUpload()
{
   $d = array(
     file_name      => '',
     file_type      => '',
     file_path      => '',
     full_path      => '',
     raw_name       => '',
     orig_name      => '',
     file_ext       => '',
     file_size      => '',
     is_image       => '',
     image_width    => '',
     image_height   => '',
     image_type     => '',
     image_size_str => ''
   );

   $this->load->model('upload/save_model');
   $this->save_model->saveUpload($d);
}

Model:
Code:
function saveUpload($d)
{
   $this->db->insert('uploads', $d);
}

Obviously this is a very simple example, but if you get an array with the indexes as the table columns you can use the ActiveRecord insert method.

You could have the method that uploads the file return an array of the information, which you then pass into another method to secure/santize, then finally call the saveUpload method.

That's my take on it, but please understand I'm a CI newb!

Everyone feel free to tell me I'm talking rubbish!

Cheers
#3

[eluser]jaswinder_rana[/eluser]
I guess that will be simplest of all. I need to add 2-3 more fields like UserID and date and I can do that in controller.
Thanks. I think I'll go with ActiveRecord method and save myself some time.

Few more questions:

- When I pass this data string to model, am I tying them really close and forcing to have my column names based on what I am using?

- if I pass that array, how will know which column is int and which is string?

- About sanitizing, I thought DB class did all that (that's where I am confused). Because if insert (or any other statement) is generated in DB class and i see it runs protect_identifiers() on columns and escape() which ultimately runs driver specific function. Is that enough sanitizing? Or should I add more?
#4

[eluser]iainco[/eluser]
When you use the Active Record insert function, you must supply an array with column names as indexes (or an object) and their respective values as the elements/variables.

But I'm not quite sure I follow you, 'cause if you're inserting data into a table, then you'll already know what column names are and they won't change.

Active Record queries escape data for you - "All values are escaped automatically producing safer queries." from user guide.
#5

[eluser]jaswinder_rana[/eluser]
Sorry, I wasn't clear. What I meant was, when I pass it $d array, it has list of columns and their values. But, some columns in table are type INT and some are type VARCHAR.

How will it know when to wrap values in ' (single quotes) for VARCHAR and when NOT to (int or when I pass, say, now() for date)?

For example
Code:
$d = array(
     file_name      => '',
     file_type      => '',
     file_path      => '',
     full_path      => '',
     raw_name       => '',
     orig_name      => '',
     file_ext       => 2.2,//DECIMAL
     file_size      => '',
     is_image       => 0,//INT
     image_width    => 11,//INT
     image_height   => 22,//INT
     image_type     => '',
     image_size_str => ''
   );
#6

[eluser]iainco[/eluser]
Well, I presume when the data is returned after upload, the different fields won't all be strings... so your array would look like this:

Code:
function saveUpload()
{
   $uploadResult = $this->_doUpload();

   $d = array(
     file_name      => $uploadResult['file_name'],
     file_type      => $uploadResult['file_type'],
     file_path      => $uploadResult['file_path'],
     full_path      => $uploadResult['full_path'],
     raw_name       => $uploadResult['raw_name'],
     orig_name      => $uploadResult['orig_name'],
     file_ext       => $uploadResult['file_ext'],
     file_size      => $uploadResult['file_size'],
     is_image       => $uploadResult['is_image'],
     image_width    => $uploadResult['image_width'],
     image_height   => $uploadResult['image_height'],
     image_type     => $uploadResult['image_type'],
     image_size_str => $uploadResult['image_size_str']
   );

   $this->load->model('upload/save_model');
   $this->save_model->saveUpload($d);
}

function _doUpload()
{
  //upload files
  return $uploadResult;
}

and the active record function will take care of the rest.
#7

[eluser]jaswinder_rana[/eluser]
AH so ActiveRecord function decides that. I am guessing some sort of type checking is done even though PHP is loose type.

Oh that will make things easy.

Thanks. Much appreciated
#8

[eluser]obiron2[/eluser]
I am having similar issues with creating forms and posting data back to the database.
I don't want the form field names to be the same as the database field names for security purposes.
The data types from the form may not match the datatypes in the database (e.g. checkbox is stored as tinyint - mySQL doesn't do booleans..)
I am in the process of designing a form model that holds all the data for building the form (input type, class, default values etc) where to get seed data from, the validation rules to be applied and where to post the data back to after validation has passed and how to transform it if it is not in the right data type.

It is sort of a combination of advanced forms object, data mapping and active record. I should have it all figured out after Xmas and will post it for critique then.

Obiron




Theme © iAndrew 2016 - Forum software by © MyBB