2008年11月16日星期日

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

Thank you for the help, that is exactly what I was looking for=)

Bart McLeod wrote:
>
> I think what he is looking for is:
>
> //insert related
> $relatedid = $relatedtb->insert($relatedvalues);
>
> //Almost like you wrote
>
> Bart
>
> 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 <mailto:info@spaceweb.nl>
> www.spaceweb.nl <http://www.spaceweb.nl> zce logo zce PHP 5 logo
>
> /Bart McLeod is a Zend Certified Engineer./
>
> Click to verify! <http://www.zend.com/zce.php?c=ZEND004591&r=218204904>
>
>
>
>
>

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

没有评论: