2008年11月11日星期二

Res: [fw-db] Method lastInsertId() not working with MySQL.

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

--
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.


Novos endereços, o Yahoo! que você conhece. Crie um email novo com a sua cara @ymail.com ou @rocketmail.com.
http://br.new.mail.yahoo.com/addresses

没有评论: