Welcome Guest, Not a member yet? Register   Sign In
pb inserting date in DB with scaffolding
#1

[eluser]flosaurus[/eluser]
Hello,

i'm trying to insert datas into my DB table using scaffolding.
My table has the following structure :

Code:
CREATE TABLE tablename (
  id int(11) NOT NULL auto_increment,
  title varchar(255) NOT NULL default '',
  description text NOT NULL,
  url varchar(255) NOT NULL default '',
  created_on datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (id),
  FULLTEXT KEY title (title,description)
) ENGINE=MyISAM;

when i try to insert some data with scaffolding. Every thing goes fine except the date wich stays to the default value : 0000-00-00 00:00:00
I want the datetime to be inserted dynamicaly into the DB.

Do i have to modify my DB structure so it works with the scaffold ?

What have i done wrong ?

Thanks for your help !
#2

[eluser]Phil Sturgeon[/eluser]
Scaffolding doesnt allow NULL or empty values. It will put a 0 in which will in tern translate to 0000-00-00, sorry. Had the same problem myself, it always uses 0 instead of NULL.
#3

[eluser]jahshuah[/eluser]
I have the same problem as flosaurus - and I put a date in the box. I think what he's asking is how does scaffolding know if the field is a datetime field or does scaffolding know what type of fields it's working with? It shouldn't be too hard to determine that the field is a date/datetime field and in turn access the 'date' helper and format the date accordingly for input into the database. Does anyone know how to do this without modifying the scaffolding class?
#4

[eluser]Michael Wales[/eluser]
So, you're saying - when entering data into the scaffolding if you enter "2007-08-03 12:00:00" into the field it won't get inserted into the database?
#5

[eluser]jahshuah[/eluser]
I'm saying that if I enter a date such as 08/03/2007, I get the standard 0000-00-00 00:00:00 instead of 2007-08-03 00:00:00, which I think would be more useful. It's a ton easier to populate than pounding out mysql datetime stamps. I don't think it's critical, but, it'd be pretty cool if you had options for date/datetime inputs.
#6

[eluser]Michael Wales[/eluser]
Scaffolding is only intended for development purposes anyways - I think it would be a shame to add any data parsing, at the framework level. This sort of functionality is easily added, but in a development environment I don't want the framework to parse and alter my data in any way - I want it inserted exactly as I typed it. If there was parsing involved that's one more step you must debug in the case of errors.

Date parsing is something you should leave to your application when you develop the end user's interface. Yahoo UI has some excellent date pickers available as do other Javascript frameworks.
#7

[eluser]jahshuah[/eluser]
I understand the intended limited use of scaffolding in a development environment only. I'm simply jumping into CI after only having had one other web framework experience being Django. In Django, you have the option of using the built-in admin backend which supplied you with similar functionality as scaffolding, however, the form elements on the add/edit/update pages were specific to the field in your database. For example, a date field had a javascript date-picker that would automatically insert into a text-field the appropriate date format for your selected database. It was really useful and I could see how that could be useful here as well.

Sure there's no problem typing out the full date/time stamp. It'd be even easier if there were other options. That's all I was getting at.
#8

[eluser]flosaurus[/eluser]
in fact i have created a basi table in my DB for some articles with a created_on date field
When i use the scaffolding process, the field is rendered as an input text field...

If i enter the date manualy with the correct format... things work fine

But i wanted to know whether it would be possible to generate this input dynamicaly (with scaffold) with the current date or timestamp so the date inserted in the db is the correct one !
#9

[eluser]opel[/eluser]
I'm having the same problem, what is tha best way to set a form so that date inserts automatically, same as you usually would in a PHP insert form?
#10

[eluser]mariowarner[/eluser]
i just had the same problem and was able to fix it. here's how i did it.

assuming i have a guestbook table with the following structure:

Code:
CREATE TABLE IF NOT EXISTS `guestbook_table` (
  `inc_no` smallint(5) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL default '',
  `email` varchar(30) NOT NULL default '',
  `comment` text NOT NULL,
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`gbno`)
) ENGINE=MyISAM ;

assuming the form validated successfully and i'm at the part where i'm adding the entry to the database, below are the codes on my controller relating to the insert query:

Code:
$datestring = "%Y:%m:%d %h:%i:%s";
$time = time();
$date = mdate($datestring, $time);            

$insert_data = array(
    'inc_no' => NULL ,
    'name' => $_POST["name"] ,
    'email' => $_POST["email"] ,
    'comment' => $_POST["comment"] ,
    'date' => $date
);

$this->db->insert('guestbook_table', $insert_data);




Theme © iAndrew 2016 - Forum software by © MyBB