drzrdc1766788 2016-01-27 12:41
浏览 68
已采纳

CakePHP 3 - 使用afterSave()中保存的数据

I have submitted a form in my view which will be processed in the controller. What normally happens is that the controller saves the edits by doing this:

if ($this->Requests->save($request)) {
    // the request have been saved.
}

Now I created another insert query to follow the activity of the editor with the afterSave() statement like so:

public function afterSave()
{
    // here I need the data submitted from $this->request->save($request));
    // how can I do this to use the data in the query?

    // insert query here.
}

I want to use the afterSave() because I want to use it for all changes made to the requests, but I can't seem to find a way to access the posted data.

The documentation says that the afterSave() contains the following parameters:

afterSave(Event $event, EntityInterface $entity, ArrayObject $options)

Do I need these to accomplish what I want? If so, how do I use those properly? because I can't seem to get any debug information to see what it contains of the save action.

The question is as follows:

How can I access the data saved with $this->Requests->save($request) in a beforeSave() or afterSave() statement to use the data in another query?

  • 写回答

2条回答 默认 最新

  • doumei2023 2016-01-27 14:05
    关注

    How can I access the data saved with $this->Requests->save($request) in a beforeSave() or afterSave() statement to use the data in another query?

    The 2nd arg of both callbacks is the entity data. Unless you need the whole request you can get all the data that was converted by the marshaller into an entity from the 2nd arg $entity. Check the documentation on the methods:

    I don't know what your problem is but doing debug($entity); there will show the entity data. If not something else is wrong in your code.

    Just use the entity for doing whatever you want there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法