Welcome Guest, Not a member yet? Register   Sign In
Active Record insert without fields and values
#1

[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
#2

[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




Theme © iAndrew 2016 - Forum software by © MyBB