0

I want to get id of inserted record in zf2. I found solution using scope_identity in php. But how to use it in zend?

My code in indexcontroller is

<?php
    public function addAction()
    {
        $form = new UserForm();

        $request = $this->getRequest();

        if ($request->isPost())
        {       
            $form->setData($request->getPost());            
            if($form->isValid())
            {   
                $data=$form->getData();         
                $this->getUserTable()->insert($data);
            }
        }
    }
     public function getUserTable()
    {
     if(!$this->userTable)
     {        
      $this->userTable = new TableGateway('eo_user',$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter')
      );
     }   
     return $this->userTable;

    }

Schema for eo_user table is

eo_user

user_id  |  username  |  user_password  | user_email  | user_status 

Here user_id is primary key with auto increment constraint.

What changes i need to do in order to find user_id of inserted record?

rack_nilesh
  • 553
  • 5
  • 18

3 Answers3

1

You can use $this->getUserTable()->getLastInsertValue(); to get last insert ID for the inserted record.

UPDATE

$this->getUserTable()->insert($data);
$insertedId = $this->getUserTable()->getLastInsertValue();
echo $insertedId; // will get the latest id
prava
  • 3,916
  • 2
  • 24
  • 35
  • should i REPLACE it with $this->getUserTable()->insert($data); or write AFTER it? – rack_nilesh Jul 30 '14 at 05:53
  • Need to write after insert. – prava Jul 30 '14 at 05:53
  • But what if another insertion is made between these two by another user? – rack_nilesh Jul 30 '14 at 05:55
  • anyway your solution worked for my script,though its not proper solution. +1.thnx – rack_nilesh Jul 30 '14 at 05:59
  • @rack_nilesh, it will give the latest ID inserted for that instance and if any other user is inserting another record, then he/she will give another ID for that insert instance. – prava Jul 30 '14 at 06:01
  • obviously it will. Is there any way to insert and get inserteId in single query,just like scope_idenity in php? – rack_nilesh Jul 30 '14 at 06:07
  • 1
    @rack_nilesh, if you will visit the method definition for `protected function executeInsert(Insert $insert)` present in `vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php`, you will find that last generated id is assigned to `$this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue()`. It assigns the last generated id for the current connection and `getLastInsertValue` method returns the same variable. – prava Jul 30 '14 at 06:13
0

After the insertion just call mysql_insert_id() function. Documentation

Edit: If this is too hard, then just do SELECT LAST_INSERT_ID(); and you're there

LHristov
  • 1,103
  • 7
  • 16
-1
    $sql = 'SELECT max(id) FROM user';  

    $query = $this->getAdapter()->query($sql);
    $result = $query->fetchAll();
    return $result[0]['max(id)']; 
user3337174
  • 149
  • 1
  • 7
  • 1
    And what if meanwhile another user made another insertion? – LHristov Jul 30 '14 at 05:49
  • i think it wont be effective way to write insert and select max queries separately,as there is possibility to execute another insert query by another user. – rack_nilesh Jul 30 '14 at 05:51