CodeIgniter Forums
insert followed by test - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: insert followed by test (/thread-40526.html)

insert followed by test - El Forum - 04-11-2011

[eluser]Mark Krieger[/eluser]
I have a pretty complex application written in CI which runs a cron from time to time. In the cron, records are gathered and processed, and they are put into the db based on a key field. The code checks if the key is already in the database, if so, it should just move along and process the next record; if the key is not in the database, it should add a new record. Simple enough.

I've found that if I have two records with the same (new) key come in one right after the other, the first does an insert just fine, but when the second record comes along and tests the key, it finds it not there, and it puts a second record with the same key into the database. (They are not used as real db indexes in my app, so this is not a disaster, but it is very annoying).

Q: When I do my first insert, I assumed the data would be written synchronously, is that not the case? (So that the later test would find the first record there). Do I need to put transaction calls around the insert? Will that ensure it is written?

I am using standard CI Active Records.