2008年11月11日星期二

[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);

--
View this message in context: http://www.nabble.com/Method-lastInsertId%28%29-not-working-with-MySQL.-tp20436556p20436556.html
Sent from the Zend DB mailing list archive at Nabble.com.

没有评论: