Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to change the LastEdited field?


Go to End


5 Posts   1531 Views

Avatar
Jare

Community Member, 39 Posts

25 November 2015 at 11:10am

Hi,

I would like to change the value of the LastEdited field of a DataObject, but it always gets overwritten when I call write(). I'm having SilverStripe 3.1.16. Thank you for your replies!

Offtopic: Is there any board for reporting issues with this particular forum (not meaning the module)? The session timeout is far too short. When I start new topics, I usually spend a lot of time creating some code samples and also doing some little research on the problem at hand before finally being satisfied with my post and pressing the Post button. I try to remember to copy paste my post before clicking that button, but if I don't remember, I usually need to start writing my post over because the session has ended and my post wasn't saved... And that's what happened to me again with this post, so I won't write the whole longer post again from scratch.

Avatar
helenclarko

Community Member, 166 Posts

25 November 2015 at 12:22pm

Hi Jare,

This should have been corrected in SS 3.0, but maybe this is something different.

I've found a snippet below which may (or may not) help out.

$this->logInAs('admin');
$page->LastEdited = '2011-11-11 17:00:00';
$page->doPublish();
$this->logOut();

Also I have been stung by the page timeout issue too, Hope someone higher up at SS sees these messages.

Avatar
Jare

Community Member, 39 Posts

17 December 2015 at 12:04am

Excuse me, what is the context when calling $this->logInAs()?

logInAs() is not familiar to me, so I checked it with PhpStorm and noticed that it is a method in the FunctionalTest class, which also is unknown to me, so I'm quite confused.

My testing code so far is inside the Page_Controller class:

public function index()
	{
		#$this->logInAs();
		$testobject = new TestDataObject();
		$testobject->Created = '2000-01-01 00:00:00';
		$testobject->LastEdited = '2000-01-01 00:00:00';
		$testobject->write();
		#$this->logOut();
		return array();
	}

I have commented out the logInAs() and logOut() calls as the are not available in Page_Controller. So setting the Created field does work, but LastEdited always gets the current timestamp.

Thanks for your help! :)

Avatar
Devlin

Community Member, 344 Posts

17 December 2015 at 12:46am

I believe this is a code snippet from the unit tests. logInAs() makes sure that we write with admin privileges in the tests. That's all.

As for your issue. SilverStripe assumes that you want to be the LastEdited values to be updated with each write() call. So everytime you use ->write(), the LastEdited value will be updated. I'm sure, there is no way around this.

If you want to update the LastEdited value in DataObjects retroactively, you'll need to that outside the ORM. For example:

$testobject = new TestDataObject();
$testobject->Foo = 'Bar';
$testobject->write();
DB::query("UPDATE TestDataObject SET LastEdited = '2000-01-01 00:00:00' WHERE ID = $testobject->ID");

Avatar
Jare

Community Member, 39 Posts

22 December 2015 at 6:39am

Edited: 22/12/2015 6:41am

Thank you, Devlin! That one solved the problem! :)

I actually made it a module that hooks to every DataObject and ensures that custom modifications to the LastEdited value are preserved after writing:

https://github.com/Taitava/changeablelasteditedvalue

The module requires no configuration, so it's very easy to use if anybody else has the same problem.