Retrieving inserted IDs from saveAll() in CakePHP

こ雲淡風輕ζ 提交于 2019-12-03 03:18:18

afterSave function is called after each individual save in a saveAll execution, so you could do: In your AppModel


class AppModel extends Model {
    var $inserted_ids = array();

    function afterSave($created) {
        if($created) {
            $this->inserted_ids[] = $this->getInsertID();
        }
        return true;
    }
}

You can place this code into any model and it should work fine. Then to return the IDs after the saveAll in your controller, you would do so like this:


if($this->Post->saveAll($posts)) {
    $post_ids=$this->Post->inserted_ids; //contains insert_ids
}

Hope it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!