Question

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).

Was it helpful?

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.

OTHER TIPS

I never used cakephp before, but with mysql and mysqli you can get the last inserted ID with the following:

mysql_insert_id(), and $mysqli->insert_id respectively.

http://php.net/manual/en/function.mysql-insert-id.php

http://php.net/manual/en/mysqli.insert-id.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top