![]() |
Active Record insert without fields and values - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Active Record insert without fields and values (/showthread.php?tid=42671) |
Active Record insert without fields and values - El Forum - 06-15-2011 [eluser]Unknown[/eluser] Hi all, Bug or Feature? I'd like to insert a record to a table without any field and value specified. (the meaning of that is to store a timestamp, so the table has an auto incremented id and a timestamp with the default value populated by mySQL) I tried this, because the insert method need a second parameter for the array of field/value pairs: $this->db->insert("mytablename",array()); Got this error: --- You must use the "set" method to update an entry. --- Following the logic of Active Record my statement need to generate something like this: INSERT INTO mytablename() VALUES () ...which is a valid SQL statement and creates a record with the default values in the fields (in my case an auto incremented ID and a timestamp) The solution what I found is: $this->db->insert("mytablename",array("ID"=>"")); which is in SQL this: INSERT INTO mytablename (ID) VALUES ("") ...which is also valid (? - not throwing error), but does not make much sense to specify in the object the auto incremented field. Any thoughts? Cheers, Tibor Active Record insert without fields and values - El Forum - 06-16-2011 [eluser]Narkboy[/eluser] Feels like a feature to me. Basically, part of Active Record is to reduce the potential for errors going into the database. If you intend to put an 'blank' record in, then it makes you code a specific blank. If you don't want blank records, this behavious means you see an error. That said, it would seem that you can still get no error if you try to enter a record but for whatever reason the data does not get there. Perhaps the best way would be to code a specific Active Record function - insert_blank() for this purpose. I can't see a reason for having a table with an id and timestamp though. Even for an access log, surely you want more information than that? I'm sure you have your reasons! /B |