And I have looked at this function!  It was a long time coming, but as of 
revision 4697, the Table::insert() method returns the *primary key* of the row 
you inserted.

This includes getting the last insert id (if you use an auto-increment key e.g. 
with MySQL), the value generated by a sequence (if you use sequences e.g. with 
PostgreSQL or Oracle), or a natural key if your table uses neither 
auto-increment or sequences.  

It also supports compound primary keys (that is, multi-column keys) and in that 
case returns an associative array of the primary key columns and values.

The insert() method returns null if it can't discover the primary key value, 
for instance if the table has no primary key, or the key value is set in a 
database server-side trigger, etc. 

So the usage is this:

  $table = my Table();
  $key = $table->insert($data);

Voila!

Regards,
Bill Karwin

> -----Original Message-----
> From: Maurice Fonk [mailto:[EMAIL PROTECTED] 
> Sent: Friday, May 04, 2007 10:47 AM
> To: Alex Netkachov
> Cc: José de Menezes Soares Neto; [email protected]
> Subject: Re: [fw-general] Retrieve ID Number
> 
> Oh hang on, I'm looking at the Zend_Db_Adapter_Abstract 
> insert method. 
> The Zend_Db_Table_Abstract insert method calls that:
> 
>     /**
>      * Inserts a new row.
>      *
>      * @param  array  $data  Column-value pairs.
>      * @return integer       The last insert ID.
>      */
>     public function insert(array $data)
>     {
>         $this->_db->insert($this->_name, $data);
> 
>         // @todo handle tables that have no auto-generated key.
> 
>         // @todo handle tables that use a named sequence instead
>         // of an implict auto-generated key.
> 
>         return $this->_db->lastInsertId();
>     }
> 
> apparently, this function had to be looked at ;)
> 
> MF
> 
> Maurice Fonk wrote:
> > That's funny, in the english manual
> > 
> (http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.inse
> > rt)
> > it says:
> >
> > "The return value is /not/ the last inserted ID, as the 
> table may not 
> > have an auto-incremented column; instead, the return value is the 
> > number of rows affected (usually 1). If you want the ID of the last 
> > inserted record, call the |lastInsertId()| method after the insert."
> >
> > That differs from the api documentation?
> >
> > MF
> >
> > Alex Netkachov wrote:
> >> Hi,
> >>
> >> insert() returns exactly you need.
> >>
> >> 
> http://framework.zend.com/apidoc/core/Zend_Db/Table/Zend_Db_Table_Abs
> >> tract.html#insert
> >>
> >> return: The last insert ID.
> >>
> >> Sincerely,
> >>
> >> On 5/4/07, José de Menezes Soares Neto <[EMAIL PROTECTED]> wrote:
> >>> Hi friends,
> >>>
> >>> I am inserting a new record in a DB, but, I would like to 
> retrieve 
> >>> the ID of the new record...
> >>>
> >>> How can I do that?
> >>>
> >>>                 $data = array(
> >>>                         'fun_nome'        => $fun_nome,
> >>>                         'fun_cpf'         => $fun_cpf,
> >>>                         'fun_cep'         => $fun_cep,
> >>>                         'fun_observacoes' => $fun_observacoes,
> >>>                         'fun_data'        => date("Y-m-d h:i:s"),
> >>>                 );
> >>>
> >>>                 $funcionarios = new Funcionarios();
> >>>                 $funcionarios->insert($data);
> >>> <------------------------------- here!
> >>>
> >>> best regards,
> >>>
> >>> José de Menezes
> >>>
> >>
> >>
> >
> >
> 
> 
> --
> Maurice Fonk
> 
> [EMAIL PROTECTED]
> http://naneau.nl/
> 
> Scio me nihil scire
> 
> 

Reply via email to