1

I want to disable the automatic behaviour of changing the 'updated_at' field when an object is updated. I want to do it manually; or at least, have the posibility to disable it as wanted.

I know I can do this by building my own behaviour as in this great answer. But I was searching for something 'cleaner' modifying a listener of the object.

  • I've tried to override the preUpdate() action on the model.
  • I've tried to disable the listeners, and nothing:

--

Doctrine::getTable('Place')->getRecordListener()->setOption('disabled', true);
// or
Doctrine::getTable('Place')->getRecordListener()->setOption('disabled', array('preUpdate'));
// As reference, I've used these two lines on a Symfony Task

Any more ideas, or code to look at?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
fesja
  • 3,313
  • 6
  • 30
  • 42

4 Answers4

4

You can access the listener directly from your object like this:


  $listenerChain = $this->getListener();

  $i = 0;

  while ($listener = $listenerChain->get($i))
  {
    if ($listener instanceof Doctrine_Template_Listener_Timestampable)
    {
      $listener->setOption('disabled', true);
      break;
    }
    $i++;
  }     
Ronald
  • 41
  • 1
  • Depending on the use case, this answer might be more relevant. As the original question was about having "the posibility to disable it as wanted". – Bonswouar Sep 16 '15 at 15:01
3

according to the docs at http://www.doctrine-project.org/documentation/manual/1_1/nl/behaviors:core-behaviors#timestampable if you want to use Timestampable but not the updated portion of it just use:

Timestampable:
  updated:
    disabled: true

And add in your own updated_at field in the columns section.

Josh Coady
  • 2,089
  • 2
  • 18
  • 25
0

The most simple way to do this would be to rename the updated_at field to something else. So that it is ignored by Doctrine.

That way, you can control the contents of the field exactly.

Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
0
// get the first (in our case the timstampable) listener for the record
$timestampable = $record->getListener()->get(0);

// disable setting of created_at at the timestampable listener
$timestampable->setOption(array('created' => array('disabled' => true)));

problem is that you have to know the position of the listener

you can also disable all listeners this way:

$record->getListener()->setOption('disabled',true);
Tobi
  • 51
  • 1
  • 1