CakePHP:保存后查找数据

时间:2014-10-09 16:06:35

标签: cakephp cakephp-2.3

我正在研究一种编辑方法。保存数据后,将发送一封电子邮件,其中包含编辑期间所做的更改。一切都有效,除了一个真气但至关重要的错误。在这里简单地提炼出来:

$data = $this->SupportTicket->readForView($st_id);
$this->SupportTicket->id = $st_id;
if ($this->SupportTicket->save($this->request->data)) {

    //call custom model method to pull view data
    $data = $this->SupportTicket->readForView($st_id);
    //do something with this data

}

问题是$ data与预存数据一起出来。那么我接下来尝试使用新数据不起作用。 我不能只使用$ this-> request->数据,因为它没有我想要的完整数据。

保存确实有效。如果我刷新同一记录的视图方法,它会显示为已更新。所以它正在保存,但是当我在保存后进行查找时,它会给我旧的数据。

有什么想法吗?

更新:findById($ st_id)不会发生这种情况,所以它必须与我的自定义方法有关。代码:

public function readForView($id)
{
    $data = $this->find('first', array(
        'conditions' => array(
            'SupportTicket.id' => $id
        ),
        'contain' => array(
            'User',
            'Owner'
        )
    ));

    if (empty($data)) {
        throw new notFoundException('Ticket not found');
    }

    $data['SupportTicket']['type_long'] = $this->getLongType($data['SupportTicket']['type']);
    $data['SupportTicket']['status_long'] = $this->getLongStatus($data['SupportTicket']['status']);
    $data['SupportTicket']['name'] = 'Support Ticket #' . $data['SupportTicket']['id'] . ' - ' . $data['SupportTicket']['title'];

    return $data;
}

将此方法中的代码复制到Controller中会得到相同的结果。

2 个答案:

答案 0 :(得分:0)

我发现这有用:https://edivad.wordpress.com/2008/04/15/cakephp-disable-model-queries-caching/

按型号:

class Project extends AppModel {
    var $cacheQueries = false;
...

按功能:

function someFunction {
  $this->Model->cacheQueries = false;
...

答案 1 :(得分:-1)

尝试使用上一个插入ID

$ ID = $这 - > getLastInsertID();

public function readForView($ id)

相关问题