1

I am trying to implement an action that can get me the last inserted id before I insert a record.

The action is basically supposed to get the last inserted id, then i add 1 to it then the value will be used in the current data been inserted.

This how far I have gotten and the error am getting

//the action to get the last inserted id
public function getLastID(){
    $lastcourseid = $this->tableGateway->select(function (Select $select){
        $select->columns(array('id'));
        $select->order('id ASC')->limit(1);
     });

    var_dump($lastcourseid);


    return $lastcourseid;
}

I call the function here before saving

        if($id == 0){
        $data['course_code'] = $this->getLastID();
        $this->tableGateway->insert($data);
    }else{
        if($this->getAlbum($id)){
            $this->tableGateway->update($data, array('id' => $id));
        }else{
            throw new \Exception("Form id does not exist");//an error is thrown in case the id is not found

        }
    }

This is the error am getting

Catchable fatal error: Object of class Zend\Db\ResultSet\ResultSet could not be converted to string I do not know where am going wrong.

Any help will be appreciated. Thanks

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • You should insert first...then use the newly provided id, assuming the id is an auto_incrementing primary key. – Orangepill Jul 30 '13 at 05:18

2 Answers2

4

There is no such thing like "last id before insert".
And you don't need it.

First insert a record and then get your id. This is how it works.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • To give some reasons to this: Imagine two users simultaneously using your website. Both users would receive "next ID = 2" or something like this. And just like that you would already have broken your application ;) – Sam Jul 30 '13 at 05:55
  • I get your reasoning and it is very valid. I have now resulted to get the last inserted id as you suggest then perform an update action to update the column. ie I have a column named course_code, i will update it with last inserted id after making an insert. Will that work? – Lawrence Macharia Jul 30 '13 at 06:42
  • @LawrenceMacharia Yes it should work and it it's how it have to be done. I hope this course_code field is in another table. Otherwise I see no use for it in the same one. – Your Common Sense Jul 30 '13 at 06:54
  • It is actually in the same table. What am trying to do is to create a unique code that has a combination of text and numbers – Lawrence Macharia Jul 30 '13 at 08:26
  • 1
    Use a composite key `$id(INT autoinc)` & `charid(CHAR)` see this [answer](http://stackoverflow.com/questions/3804845/auto-increment-usage-in-composite-key) – david strachan Jul 30 '13 at 09:39
0

You do not mention the underlying database (postgressql, mysql, etc.). I am more familiar with MySQL. The Perl and php AIP's have "last row id" functions. For example, php has "mysqli_insert_id()". This assumes that your rowID is an AUTO_INCREMENT column. Other DBs may have different requirements.

7 Reeds
  • 2,419
  • 3
  • 32
  • 64