Sunday, February 9, 2014

Re: Date modified isn't null when adding a record

Well, that is not strange, that is exactly how it is supposed to be
"adding" a record is also a type of modification

if you need to know if the record has been modified yet, compare created and modified.
if they are equal the records have not been yet modified.

mark


Am Sonntag, 9. Februar 2014 15:46:34 UTC+1 schrieb Sam Clauw:

Normally, when you add a record with a form to a database table, CakePHP will automatically fill in the "created" field in de database with the current date & time. It works for my table called "attractions" (model "Attraction").

But now, strange things are happening. When I add a record for:

  • model "AttractionProperty", table "attraction_properties"
  • model "AttractionTypes", table "attraction_types"
  • model "AttractionPropertyLink", table "attraction_properties_links"
  • ...

... both the fields "created" and "modified" are filled in. I checked if the id of my model is set in the add action ($this->request->id), but it says "false".

Here's my controller code:

public function add()
{
    if ($this->request->is('post')) {
        $this->AttractionProperty->create();
        if ($this->AttractionProperty->save($this->request->data)) { // data array opslaan
            $this->Session->setFlash(__('De eigenschap werd succesvol toegevoegd.'), 'default', array(
                'class' => 'alert alert-success'
            ));
            return $this->redirect(array(
                'action' => 'index'
            ));
        }
        $this->Session->setFlash(__('Er is een fout tijdens het toevoegen van de eigenschap opgetreden.'), 'default', array(
            'class' => 'alert alert-danger'
        ));
    }
}
public function edit($id = null)
{
    if (!$id) {
        throw new NotFoundException(__('Ongeldige eigenschap.'));
    }
    $property = $this->AttractionProperty->findById($id);
    if (!$property) {
        throw new NotFoundException(__('Ongeldige eigenschap.'));
    }
    if ($this->request->is(array('post', 'put'))) {
        $this->AttractionProperty->id = $id;
        if ($this->AttractionProperty->save($this->request->data)) { // data array opslaan
            $this->Session->setFlash(__('De eigenschap werd succesvol bewerkt.'), 'default', array(
                'class' => 'alert alert-success'
            ));
            return $this->redirect(array(
                'action' => 'index'
            ));
        }
        $this->Session->setFlash(__('Er is een fout tijdens het bewerken van de eigenschap opgetreden.'), 'default', array(
            'class' => 'alert alert-danger'
        ));
    }
    if (!$this->request->data) {
        $this->request->data = $property;
    }
}


I did read http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array to retrieve more info, but it seems that I've done everything to the rules. I also know that you can do 

if ($this->AttractionProperty->save($this->request->data(array('modified' => false)))) {
    
}


when you save in the "add" action, but I realy want to know what's the underlying problem ;)

Is this a common problem or what could be the reason of this behavior?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: