【问题标题】:Getting LastInsertId from SimpleTest TestAction in CakePHP从 CakePHP 中的 SimpleTest TestAction 获取 LastInsertId
【发布时间】:2012-03-09 22:05:24
【问题描述】:

我正在通过 SimpleTest 在 CakePHP 1.3 中编写单元测试。

我运行以下代码,它可以正常工作(它确实按预期创建)

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

我需要找到添加的帖子的 ID,以便在接下来的两个测试中使用 - 编辑帖子和删除帖子。我尝试过通过模型进行引用(即 $Post->id、$this->Post->id),但似乎通过 testAction 的方式不同。

我也意识到我可以查询以找到 max(id),但是如果恰好测试系统上的另一个用户在几乎同时插入了一个新帖子,它可能会搞砸结果(至少据我所知 - 如果我错了,请纠正我)。

【问题讨论】:

    标签: php cakephp simpletest


    【解决方案1】:

    通常,您应该使用控制器

    $this->Post->getLastInsertID();
    

    这是连接器方法的包装器。

    $this->Post->id 
    

    用于设置一个id,然后检索或设置数据。

    【讨论】:

      【解决方案2】:

      我以前从没用过 cakephp,但是使用 mysql 和 mysqli 你可以得到最后插入的 ID,如下:

      分别为mysql_insert_id()$mysqli->insert_id

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 1970-01-01
        • 2014-05-12
        • 2010-11-24
        • 1970-01-01
        相关资源
        最近更新 更多