I have a model (Addresses) with the following fields:
id: char(36) #using uuid
name: varchar(150)
#city, state, etc here
created: timestamp
modified: timestamp
In my AddressesTable class I have:
public function initialize(array $config)
{
$this->table('addresses');
$this->addBehavior('Timestamp');
}
In my controller, I have this in the edit method:
public function edit($id = null) {
$addressesTable = TableRegistry::get('Addresses');
$address = $addressesTable->get($id);
if ($this->request->is(array('post','put'))) {
$address = $addressesTable->patchEntity($address, $this->request->data);
if ($addressesTable->save($address)) {
$this->Flash->success('The address has been saved.');
return $this->redirect(['action' => 'addresses']);
} else {
$this->Flash->error('The address could not be saved. Please, try again.');
}
}
$this->set(compact('address'));
}
The problem I'm running into is this. According to everything I've read, this should 'update' the record (which it does), and update the 'modified' field in the DB to the current timestamp (which it also does). However, in addition, it also updates the created timestamp (which, it should NOT do).
What am I missing here?
I need this to update ONLY the modified column and NOT the created column on save.
I had the same problem using CakePHP 3.4. I solved using this in my Model/Table:
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
}
More informations here: https://book.cakephp.org/3.0/en/tutorials-and-examples/blog/part-two.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With