5

I've got two little questions:

actAs: { Timestampable: ~ }

What the "~" what mean in the code above?

Then, I've seen that tables with actAs: { Timestampable: ~ } have two fields (created_at and updated_at).

Is it possible to bind the updated_at field to a particular field (I update this field, then updated_at get a new value) ?

satboy78
  • 217
  • 1
  • 5
  • 14
  • `updated_at` is generally used to indicate that *any* field on the record was changed. Consider creating a separate field for the purpose you've described. –  Apr 26 '12 at 23:00

1 Answers1

14

The "~" means that you will use default values or default configuration. In your case, the behavior Timestampable, will use the default value and configuration. So you don't have to redefine them.

From the doc, here are some configuration:

Timestampable:
  created:
    name: created_at
    type: timestamp
    format: Y-m-d H:i:s
  updated:
    disabled: true

You will also find this "~" (a lot) in the default generator.yml. This way, the generator, even empty, will generate a nice admin:

config:
  actions: ~
  fields:  ~
  list:    ~
  filter:  ~
  form:    ~
  edit:    ~
  new:     ~

For your second question, the goal of the Timestampable is for each modification on a row, the field updated_at will be set with the current date. So you don't need to take care of it.

Edit:

And if you want to manually update the updated_at field:

  • first: you will have to disable the timestampable behavior for this field (see the example above
  • second: you will have to do the behavior on your own.

The easiest way is to extends the preSave function of your model and do the job here. Like:

class Article extends BaseArticle
{
  public function preSave($event)
  {
    if(array_key_exists("your_field", $this->getModified())
    {
      $this->setUpdatedAt(time());
    }
  }
j0k
  • 22,600
  • 28
  • 79
  • 90