Search code examples
phpcakephpsimpletest

Getting LastInsertId from SimpleTest TestAction in CakePHP


I am writing a unit test in CakePHP 1.3 via SimpleTest.

I run the following code, which works (it does create as expected)

$this->testAction('/post/add/user:'. $userId, array('method' => 'post'));

I need to find the ID of that added post to use in the next two tests - editing a post and deleting a post. I have tried referencing through the model (i.e. $Post->id, $this->Post->id) but it appears that going through testAction doesn't work the same way.

I also realize that I could query to find max(id), but then if there just happens to be another user on the test system who inserts a new post at almost the same time, it could screw up the results (at least as far as I know - correct me if I am wrong).


Solution

  • Usually, from a controller you should use

    $this->Post->getLastInsertID();
    

    This is a wrapper for connectors methods.

    $this->Post->id 
    

    is used to set an id and then retrieve or set data.