symfony: actAs: { Timestampable: ~ }
Asked Answered
W

1

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) ?

Wilberwilberforce answered 26/4, 2012 at 18:37 Comment(1)
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.Stickweed
N
15

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());
    }
  }
Nalor answered 26/4, 2012 at 19:14 Comment(2)
ok, perfect thanks. I would like to update the update_at only if only one particular field of my table is modified: is it possible?Wilberwilberforce
To clarify, ~ is YAML for null.Innings

© 2022 - 2024 — McMap. All rights reserved.