开发者

Active Record in CodeIgniter Incrementing More Than It Should

开发者 https://www.devze.com 2023-02-16 06:43 出处:网络
I posted this in the CodeIgniter forums the other day and haven\'t recieved a response, so I\'m trying here. It appears that for some reason, my auto increment value in my table is being incremented b

I posted this in the CodeIgniter forums the other day and haven't recieved a response, so I'm trying here. It appears that for some reason, my auto increment value in my table is being incremented by 2 instead of 1 after I do a delete statement followed by an insert statement.

Here the is the create syntax for my table.

CREATE TABLE `brokerage_zip_range` (
     `id` int(11) NOT NULL auto_increment,
     `zip_code` varchar(10) NOT NULL,
     `brokerage_id` int(11) NOT NULL,
     PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 

I have code that looks like this in a model:

public function assign_zip_range($brokerage_id, $zip_code) {
     $this->db->where('brokerage_id',$brokerage_id);
     $this->db->delete('brokerage_zip_range');

     $this->db->set('brokerage_id', $brokerage_id);
     $this->db->set('zip_code', $zip_code);
     $this->db->insert('brokerage_zip_range'); 
} 

(Please note that for reasons I won’t get into here, I can’t just make this an update statement).

The problem I am having is that while this code executes fine, the inserted record is incremented by more than one. So, prior to the execution of this code, the record might look like:

id    zip_code brokerage_id
7     95202     2

After I run the code (say, with zip code 92222), it now looks like:

id    zip_code brokerage_id
9     92222     2 

The next auto increment value should be 8, not 9.

Am I doing something wrong or is this a CI thing? I will point开发者_Go百科 out that I have tried running this code by itself in CI, meaning there are no other operations happening at all before or after it. If it makes a difference (and I doubt it does) I am using CI 2.0/MySQL 5.01/PHP 5.3.

UPDATE! I FOUND THE ANSWER So in the end, it turns out that a custom MY_Router class I was using (so I could have more than one level of controllers without having to resort to manual routing) was apparently calling everything twice. I noticed this when I went and worked on a completely different section of the code, and saw that it was inserting two records of the same data instead of one. I removed that class and now everything is fine again.

Thank you all for your help.


I really really doubt that it is CI doing this. Try outputting the SQL generated by the CI AR by:

$this->db->last_query();

so that you can see what it SQL it runs on your database.


For others with Flash content in their view:

I found out that Flash player was forcing my page to make a second request. In my case it was loading a video. The solution to my problem was to place the flash embed into a separate html file and load that html file into an iframe in the offending view.


Not directly answer your question. But, try profiling your app by adding:

$this->output->enable_profiler(TRUE);

on your __construct()

and see if it's indeed only one INSERT call.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号