简体   繁体   中英

How can I use Doctrine to Get AutoIncrement Value to Sync Tables? (Or is it even possible?)

I have two tables - one temp, one perm. Periodically, I need to make sure nothing has gone awry and the auto increment values aren't out of whack. In mysql, I use

$sql = "SELECT AUTO_INCREMENT FROM  INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = :db AND TABLE_NAME = :table";

And, if necessary, I use:

 $sql = "ALTER TABLE temp_" . $table . " AUTO_INCREMENT=" . $live_ai;

However, I am trying to use sqlite for my testing, and would like a way to do this in doctrine rather than raw sql queries.

This probably won't answer your question but it's just an idea. What if you don't touch the auto-increment field at all?

I'm thinking that you could have your temp table with its own auto-increment field just for storing your temp data. Whenever you sync up or copy the data to your main table, just omit the primary key, let the main table maintain it's own auto-increment field.

If you need a list of rows that you inserted you could query the main table using a combination of related keys (eg user id) and a date_created or date_modified field.

I'm not clear on the particulars of your application but if you need to sync up data you could try adding an extra column used for versioning. This is usually a TIMESTAMP but it could be an just an number you maintain.

I've never had the need for or seen anybody try to sync up an auto-increment field, this might be a sign that there should be another way or maybe a better way to solve your problem.

I hope this helps. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM