1
votes

I'm working on an edit method. After saving data, an email is sent out with the changes made during the edit. Everything works except for one infuriating but crucial bug. Here it is distilled down very simply:

$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

}

The issue is that $data comes out with the pre-save data. So what I then try to do with the new data doesn't work. I can't just use $this->request->data because it doesn't have the full data that I want in it.

The save does however work. If I refresh the view method for the same record, it shows as updated. So it's saving, but when I do the find after saving it is giving me old data.

Any ideas?

Update: it doesn't happen with findById($st_id) so it must be something to do with my custom method. Code:

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;
}

Copying the code from this method into the Controller gives the same result.

2
Are you by any chance using query caching (Model::$cacheQueries)? - ndm
Agree, @ndm - there has to be some kind of caching happening. - Dave
Yes, that's it! I didn't write the app originally and hardly look in AppModel. - gazareth
@ndm if you post it as an answer I'll gladly accept. - gazareth
What elegant way would you propose for switching it off on a find-by-find basis? I found this but it's specific to Cake 1.x and my OOP-fu is not strong enough to figure it out for 2.x. bakery.cakephp.org/articles/dennis.hennen/2009/09/15/… - gazareth

2 Answers

0
votes

I've found this helpful: https://edivad.wordpress.com/2008/04/15/cakephp-disable-model-queries-caching/

By model:

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

By function:

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

try using last Insert ID

$id=$this->getLastInsertID();

public function readForView($id)