Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony: actAs: { Timestampable: ~ }

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

like image 320
satboy78 Avatar asked Apr 26 '12 18:04

satboy78


1 Answers

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());
    }
  }
like image 193
j0k Avatar answered Oct 09 '22 05:10

j0k