Welcome Guest, Not a member yet? Register   Sign In
Best practice for formatting data before being entered into a database
#1

[eluser]digitalbloke[/eluser]
Hi there!

I'm new to CodeIgniter and have been watching many video tutorials and I really like what I see! With that in mind, I set about developing a project site to help develop my CI skills.

I had a quick question about the best way to format a string before entering it into the database.

For example, I have a News section in my project, with a controller that allows me to create a news story. In the form there is a field to allow me to set the date the story is to be published at. Should I run human_to_unix() on this field within the controller, and then pass it data to the model or, pass the data to the model and have the model run human_to_unix()?

Thanks in advance!
#2

[eluser]überfuzz[/eluser]
I'd say it's a matter of taste. I like to store all time related stuff in unix_timestamp. What I like about that is that I get virtually all that there is to know about the time stated in a simple int-value. And of course, it what I feel accustom with.
#3

[eluser]überfuzz[/eluser]
By the way, not to forget. Welcome to the CI forum, hope you'll like it!
#4

[eluser]digitalbloke[/eluser]
[quote author="überfuzz" date="1258941691"]By the way, not to forget. Welcome to the CI forum, hope you'll like it![/quote]
Love it!

Hi überfuzz!

Thanks, I like the unix time stamp too Smile but obviously my users are not going to enter unix time stamps in a form. What I was really after is where would you convert the human input to a unix time stamp?

In the Controller or the Model?
#5

[eluser]tkyy[/eluser]
time() will give you the timestamp in unix format right now. there are native php functions to convert date format's to unix timestamps (i have used this before coding a "browse" page on social web stuff, browsing by age is a little more tricky than you would think).

hope this helps.
#6

[eluser]tkyy[/eluser]
oh and please do not put that kind of calculation in a model, use models almost only for database queries. i would actually put this type of conversion in a library, and then put the actual logic in the controller.
#7

[eluser]digitalbloke[/eluser]
Thanks for the reply tkyy.

Sorry, I don't think I made it very clear what I was after. I understand about the PHP date functions, I'm just wondering where is the best place to put them. See the lines commented 'convert date' in the two examples below. Which would you use?

N.B. Obviously this is a very simple version of the code (there may even be errors!)

Example 1:

Controller:
Code:
class News extends Controller
{
    function create()
    {
        $news['title'] = $this->input->post('title');
        $news['date'] = $this->input->post('date');
        
        // convert date
        $news['date'] = human_to_unix($news['date']);
        
        $this->news_model->create($news);
    }
}

Model:
Code:
class News_model extends Model
{
    function create($data)
    {
        $this->db->insert('news', $data);
    }
}

-------------------

Example 2:

Controller:
Code:
class News extends Controller
{
    function create()
    {
        $news['title'] = $this->input->post('title');
        $news['date'] = $this->input->post('date');
        
        $this->news_model->create($news);
    }
}

Model:
Code:
class News_model extends Model
{
    function create($data)
    {
        // convert date
        $data['date'] = human_to_unix($date['date']);
        
        $this->db->insert('news', $data);
    }
}

Thanks in advance!
#8

[eluser]überfuzz[/eluser]
Well let's put it this way. Ask yourself, do I want to use the function locally or throughout the entire site? I guess I'd go for tkyys advice on this matter. Make a function an put it in the library.
Example:
libraries/Nifty_time_functions.php
Code:
class Nifty_time_functions
{
  function stamp_to_human($stamp)
  {
     //code
     return $human;
  }

  function human_to_stamp($time)
  {
     //code
     return $stamp;
  }
}

Now in your controller you'll be using your custom time functions like this.
Code:
$this->load->library('Nifty_time_functions');
$unix_timestamp = $this->nifty_time_functions->human_to_stamp($five_a_clock);

Edit, corrected the class name.




Theme © iAndrew 2016 - Forum software by © MyBB