zend framework - INSERT and UPDATE the same row in the same TRANSACTION? (MySQL) -
so here's problem:
i have article submission form optional image upload field.
when user submits form - happens:
if($this->view->form->isvalid($_post){ $db->begintransaction(); try{ // save content of post article table if(!$this->_savearticle($_post)){ return; } // resize , save image using id generated previous condition if(!$this->_saveimage($_files){ $db->rollback(); return; } // update record if image generated if(!$this->_updatearticle(){ $db->rollback(); } $db->commit(); } }catch (exception $e){ $db->rollback() }
all models saved using mappers, automate "upsert" functionality checking existence of surrogate key
public function save($model){ if(!is_null($model->id_article){ $mapper->insert($model->getfields()); return; } $mapper->update($model->getfields(),$model->getidentity()); }
the article table has composite unique index of id,title , url. in addition, i'm generating uid gets added id field of model prior insert (instead of auto-incrementing)
when try execute this, runs fine first article inserted table - subsequent calls (with radically different input) triggers duplicate key error. mysql throws id generated in condition 1 (_savearticle) , complains key exists...
i've dumped out model fields (and condition state - i.e. insert | update) , proceed expected (pseudo):
inserting! id = null title = content = image = null updating! id = 1234123412341234 title = content = else image = 1234123412341234.jpg
this row data not present in database.
i figure 1 of few things:
1: i'm loading secondary db adapter on user login, allowing them interface several sites 1 login - might confusing transaction somehow
2: it's bug of description in zend transaction implementation (possibly triggered 1)
3: need replace save() insert ... on duplicate
4: should restructure submission process, or generate name image isn't dependent on uid of inserted row.
still hunting, wondering if else has encountered kind of issue or point me in direction of solution
best swk
ok - record, entirely possible. problem in application architecture. catching exceptions in mapper classes handling persistence - , querying them return boolean states , interrupt process. in turn breaking try/catch loop preventing insert/update working correctly. summarise - yes - can insert , update same row in single transaction. i've ticked community wiki cancel rep out
Comments
Post a Comment