我正在通过简单的最终在Cakephp 1.3中编写一个单元测试。

我运行以下代码,它有效(它确实像预期创建)

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

我需要找到在接下来的两个测试中使用的添加帖子的ID - 编辑帖子并删除帖子。我尝试通过模型引用(即$ POST-> ID,$以下 - >后 - > ID),但似乎通过TESTACTION的结果不同样地工作。

我也意识到我可以查询以查询最大(ID),但是如果刚刚发生在测试系统上的另一个用户,他们在几乎上插入新帖子可以搞砸结果(至少据我所知 - 如果我错了,请纠正我)。

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top