Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 3 created and modified not working

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.

like image 370
user4838338 Avatar asked Nov 20 '25 22:11

user4838338


1 Answers

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

like image 138
3 revs, 2 users 80% Avatar answered Nov 24 '25 12:11

3 revs, 2 users 80%



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!