//insert related $relatedid = $relatedtb->insert($relatedvalues); //Almost like you wroteBart
Luiz Fernando schreef:
Hi, I don't understand at all. But if the Hs_Db_Table extends Zend_Db_Table, you unable to call ->lastInsertId(), this is a Zend_Db_Adapter method. To retrieve a primary key from a Zend_Db_Table (Hs_Db_Table) using the ->insert(...) method do this: //insert data to database: $inserted = $maintb->insert($mainvalues); //$inserted is a array with the columns/values $mainid = $inserted['your_pk_field']; I think that is help. Best regards, Luiz Fernando ----- Mensagem original ---- De: Antonio08 <hamed_hawwari@hotmail.com> Para: fw-db@lists.zend.com Enviadas: Terça-feira, 11 de Novembro de 2008 7:07:08 Assunto: [fw-db] Method lastInsertId() not working with MySQL. Hi, I am currently developing a website with Zend Framework and MySQL DBMS. Creating a form and a subform based on two tables related in a one-to-many relationship. After inserting the information in the main table which has a PK of Autonumber attribute, I attempted to use the lastInsertId() method to retrive the PK of the main table to be able to insert it into the subtable to create the relationship. Sadly it constantly comes up with an error. Any thoughts, suggestions, or ideas on how to work around this problem? Thank you. Here is the code (in bold the where the method call): //put all the values of the form into a variable, take out the 'submit' value, as we don't want to put that in the database $mainvalues = $bloodcollectionform->getValues(); unset($mainvalues['submit']); $relatedvalues = $bloodcollectionform->getSubForm('bloodcollectionsubform')->getValues(); unset($mainvalues['']); //inisiate db object //$tb = new Hs_Db_Table('HTRU_DB', 'animalOrder'); $maintb = new Hs_Db_Table('HTRU_DB', 'bloodSupplyRequestForm', $dbConfig); $rowset = $maintb->fetchAll(); $this->view->rowset = $rowset; $maintb = new Hs_Db_Table('HTRU_DB', 'bloodSupplyRequestForm', $dbConfig); $relatedtb = new Hs_Db_Table('HTRU_DB', 'bloodCollection', $dbConfig); //insert data to database: $maintb->insert($mainvalues); //TODO this does not work. get primary key $mainid = $maintb->lastInsertId(); //insert related $relatedtb->insert($relatedvalues); //TODO this does not work. get primary key. $relatedid = $relatedtb->lastInsertId(); echo "[$relatedid]"; $where = $relatedtb->getAdapter()->quoteInto('BloodCollectionId = ?', $relatedid); $relatedtb->update(array('bSRFId' => $mainid), $where);
--
| Bart McLeod Space Web Internet Team Middenlaan 47 6865 VN Heveadorp The Netherlands t +31(0)26 3392952 m 06 51 51 89 71 @ info@spaceweb.nl www.spaceweb.nl | Bart McLeod is a Zend Certified Engineer. |
没有评论:
发表评论