views:

119

answers:

3

This has to be simple... I'm trying to preserve the current date that is already stored in an updated_at field in the DB.
I'm updating a single field value in the same row and then saving but I don't want it to update the updated_at field. I want to preserve the existing updated_at value.

I only want to do this in one, maybe two situations so I don't need to overwrite the model for every other action in the program.

I've tried this:

$originalDate = $this->getUpdatedAt();
$this->setUpdatedAt($originalDate);
$this->save();

This seems like it should work but still it seems to update the field.

A: 

In symfony, usually, when a table has a column called *created_at*, it is used to store a timestamp of the date when the record was created. The same applies to *updated_at* columns, which are to be updated each time the record itself is updated, to the value of the current time.

The good news is that symfony will recognize the names of these columns and handle their updates for you. You don't need to manually set the *created_at* and *updated_at* columns; they will automatically be updated.

If you want to keep information other *created_at* and *updated_at* you should create another field.

ranonE
Is there a way to overwrite this behavior when needed though? I don't want to change the DB structure and the entire model just for a single behavior.
Failpunk
@Failpunk: By default, classes stored in the "lib" directories in your projects benefit from the autoloading automatically. You can find out more about these folders on official documentation.
ranonE
A: 

Well it seems this slight change works:

$originalDate = $this->getUpdatedAt();
$this->save();

$this->setUpdatedAt($originalDate);
$this->save();

But there has to be a more elegant way so that you don't have to do two separate saves.

Failpunk
A: 

The updated_at column only gets set with the current time if it has not been changed. This is checked with the isColumnModified method. You can trick the object in believing this has been modified if you first change it to some value, and then change it back to the original value.

$updatedAt = $book->getUpdatedAt();
$book->setUpdatedAt(null);
$book->setUpdatedAt($updatedAt);

$book->setInStock(4);
$book->save();

In Symfony 1.2, this behavior is added in SfObjectBuilder::addSave(), and cannot be disabled (it checks for a column named updated_at or updated_on). In Symfony 1.3 and 1.4, the newer behaviors of Propel 1.4 are used, so I think you have to explicitly add them to get the effect. The same trick works to circumvent the updating.

Jan Fabry