Welcome Guest, Not a member yet? Register   Sign In
Datamapper Automated Timestamp Issue
#11

[eluser]akachrislee[/eluser]
no change. Sad
#12

[eluser]naren_nag[/eluser]
If you give me a SQL statement of your DB structure, I will write out a MVC and give it to you in around six hours time.
#13

[eluser]akachrislee[/eluser]
I found something interesting, i'm not sure if its this is supposed to happen but...
When I also create a new guardian (inserting as opposed to updating), both the created and updated fields are being populated. 
is this suppose to happen? 

My understanding is that when you do an insert, it will fill in the created field. and when it does an update it will fill in the updated field. 
#14

[eluser]naren_nag[/eluser]
Code below. You'll still have to fill in the blanks.

I think the reason you're getting the error is because your view most probably has created_on and updated_on as hidden/text input fields and these get posted and saved in your current controller.

I've suggested a different, and definitely more secure approach, which will also help you avoid XSS attacks.

Read the code and comments, try it out and let me know if you have a problem.

cheers,

Naren

The model:

Code:
<?php

// models/Guardian.php

class Guardian extends Datamapper {

    function __construct($id = NULL) {
        parent::__construct($id);
    }
    
}
?>

The controller :

Code:
<?php

class Admin extends Controller {


    function edit($id = NULL) {                                // To create a new guardian call admin/edit, to edit an existing guarding
                                                            // call admin/edit/guardian_id
        
        if( ! $_POST ) {                                    // If a form has not been posted, we load the edit view
            

            if(isset($id)) {                                // If an id has been passed, then we load the values, else new Guardian
                
                $guardianObject = new Guardian($id);            
                
                if($guardianObject->exists()) {
                
                    $data = $guardianObject->to_array();
                    $this->load->view("edit", $data);
                
                } else
                    show_error("No Guardian with id $id found!");
            } else
                $this->load->view("edit");                    // Load the view without any data for a new record
        
        
        
        } else {                                            // This is the code block that gets called if a form has been posted
            
            
            $id = $this->input->post("id");
            if($id > 0)
                $guardianObject = new Guardian($id);        // Load existing data
            else
                $guardianObject = new Guardian;
            
            
            // I don't use a from_array because there's a bunch of text fields
            // that I want to XSS clean. Otherwise you're opening yourself up to
            // a serious security breach
            
            $guardianObject->first_name = $this->input->post("first_name", TRUE);            // The ", TRUE" means CI will XSS clean the input
            $guardianObject->last_name = $this->input->post("last_name", TRUE);
            
            // ... all the others as well besides created_on and updated_on (you shouldn't have these fields in your view at all!)
            // and yes, I know it's more work, but it's worth it :)
            
            $guardianObject->save();
            
            redirect("admin/view/$guardianObject->id");
        }
    }
    
    
    function view($id = NULL) {
        
        if(! isset($id))
            show_error("Don't know what to show");
        
        $guardianObject = new Guardian($id);
        
        if(! $guardianObject->exists())
            show_error("No Guardian with id $id found!");
        
        $data = $guardianObject->to_array();
        $this->load->view("view", $data);
    }

}

The view

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

&lt;head&gt;
    &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8"&gt;
    &lt;title&gt;Edit Guardian&lt;/title&gt;
    
&lt;/head&gt;

&lt;body id="guardian_edit"&gt;
    
    &lt;?echo form_open("admin/edit"); ?&gt;
    
    &lt;input type="hidden" name="id" value="&lt;?echo isset($id) ? $id : 0; ?&gt;" id="id"&gt;
    
    <label for="first_name">First Name</label>&lt;input type="text" name="first_name" value="&lt;?echo isset($first_name) ? $first_name : ""; ?&gt;" id="first_name"&gt;
    
    <label for="last_name">Last Name</label>&lt;input type="text" name="last_name" value="&lt;?echo isset($last_name) ? $last_name : ""; ?&gt;" id="last_name"&gt;
    
    &lt;input type="submit" name="Submit" value=" Save " id="Submit"&gt;
&lt;/body&gt;
#15

[eluser]dmeza[/eluser]
We're getting the same error in mac and pc xp, mysql 5.0.77 and php 5.1.6.
The error does not show up in our dev server in linux or other pc vista envs.

We're using the standard 'created' and 'updated' names in the mysql tables with no configuration for their names anywhere in the models or config files.
#16

[eluser]akachrislee[/eluser]
what type is the column updated in your database? it should be datetime. also not sure if you need the +0000 at the end. of the timestamp. 
#17

[eluser]dmeza[/eluser]
[quote author="akachrislee" date="1267494740"]what type is the column updated in your database? it should be datetime. also not sure if you need the +0000 at the end. of the timestamp. [/quote]

"created" and "updated" are datetime mysql datatypes. For what I've read in some other places the +xxxx is the timezone.
The solution is in this post and it seems to be a known issue. http://ellislab.com/forums/viewthread/112904/P679/
Quote:
...............
That’s the timezone. It’s is part of the standard, ISO accepted formats for timestamps. Either you live in the GMT/UTC time zone, or your server is incorrectly configured, which is why you see a TZ of +0000. Mine, for example, is -0500.

If your database cannot accept timezone values, you’ll have to manually change DMZ (this is going to be a configuration option in the future). Open libraries/datamapper.php, scroll to the save function, and change the two occurrences of ‘Y-m-d H:iConfused O’ to ‘Y-m-d H:iConfused’.
.................
#18

[eluser]dmeza[/eluser]
By the Way that did fix my problem.
#19

[eluser]ciGR[/eluser]
By method name, I understand that you want to update an already existing entry, I think you must use
Code:
$g = new Guardian($id);
instead of
Code:
$g = new Guardian();

EDIT

I did not see the last answers.
#20

[eluser]baba[/eluser]
i had the same issue. just changed
Code:
$config['timestamp_format'] = 'Y-m-d H:i:s';

in application/config/datamapper.php and it works fine now.
hope this helps




Theme © iAndrew 2016 - Forum software by © MyBB