Getting block updated date or added date doesn't work

Permalink 1 user found helpful
I need to get the date added and updated for individual blocks so I can tell which elements on the page have been recently added/modified and tweak the formatting accordingly. As far as I can tell the Block object doesn't return an accurate added or updated date.

There is a method for the Block object, so that's not the issue:
https://documentation.concrete5.org/api/8.4.5/Concrete/Core/Block/Bl...

The problem is the data in the database gets updated every time a new page version is created. Attached a screenshot from phpMyAdmin. In the "blocks" table, the columns "bDateAdded" and "bDateModified" are always the same. Pretty useless.

Searching around I found this post, but it is only relevant to older versions of C5 and doesn't work anymore:
https://stackoverflow.com/questions/13058445/how-can-i-determine-the...

I'm hoping I'm not the only one who is in this situation. Does anyone have a workaround?

1 Attachment

jb1
 
mnakalay replied on at Permalink Reply
mnakalay
The idea is to grab the first iteration of the block to get the date added from it (oldest bID) and the current one to get the date of last update (newest bID)

This work for me. I'm assuming you're putting this code in the block's controller
$currentBlockObject = $this->getBlockObject();
$firstBlockID = $currentBlockObject->getBlockRelationID();
$firstBlockObj = \Concrete\Core\Block\Block::getByID($firstBlockID);
$dateAdded = $firstBlockObj->getBlockDateAdded();
$dateUpdated = $currentBlockObject->getBlockDateLastModified();

Of course make sure to add code to check that you do have objects before calling functions on anything so you don't get errors.
jb1 replied on at Permalink Reply 3 Attachments
jb1
Hey Nour,
Thanks for getting back to me. I had guessed that was the purpose of the relation ID and had already gone up that path, but it proved to be a dead end for me.

I've attached 3 screenshots from my database in the "blocks" and "collectionversionblocks" tables.

It shows the current block ID is 1513 and the cbRelationID is 988. When I look up that "original" block ID it is a completely different block with a different btID (block type ID). The date/time added/updated also didn't match up.

When I dug through the database to find what that 988 could refer to, I found nothing there.

The concrete5 API doco doesn't shed much light on it. When I search through all the code in the core for the method "getBlockRelationID", it seems to point to being used on the page type defaults (in update_from_type.php and Block.php). But that doesn't make a lot of sense as I've just added a brand new "content" block to the page in question - it's not editing one that was already in the page type default.

Any other ideas?
mnakalay replied on at Permalink Best Answer Reply
mnakalay
You are right. I made a stupid mistake. As its name implies, the block relation ID is not a block ID it is the ID of the relation :)

So this should work
$currentBlockObject = $this->controller->getBlockObject();
$blockRelationID = $currentBlockObject->getBlockRelationID();
$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();
$db = $app->make('database')->connection();
$originalBlockID = $db->GetOne('select min(bID) as bID from CollectionVersionBlocks WHERE cbRelationID=?', [$blockRelationID]);
$firstBlockObj = \Concrete\Core\Block\Block::getByID($originalBlockID);
$dateAdded = $firstBlockObj->getBlockDateAdded();
$dateUpdated = $currentBlockObject->getBlockDateLastModified();
echo 'date added ' . (string) $dateAdded . '<br>';
echo 'date updated ' . (string) $dateUpdated . '<br>';
jb1 replied on at Permalink Reply
jb1
This works! Thank you very much. :-)
In the long-run I'm still not entirely clear on the relation ID considering the bID doesn't change either and in theory could be swapped out for the "cbRelationID" column in the SQL statement. Maybe there's some kind of situation where the block ID would change (such as a site migration or multilingual site) - I'm not sure.
Any chance I can have your Paypal email address so I can send you a tip in gratitude?
mnakalay replied on at Permalink Reply
mnakalay
the bID does change quite a few times actually. Every time you edit the block the bID changes. When you apply a template I believe it changes again.

If you look at the CollectionVersionBlocks table things get clearer. Do a search for any cbRelationID and you will see a list of blocks with different bID and all the same cbRelationID. Those are all the same block for which the bID changed over time.

the cbRelationID just shows that, since it is the same, those are all iterations of the same exact block.

Thank you for the nice offer I really appreciate it. It's not necessary though, on the forum, I'm happy to help, especially when I'm also curious about the problem at hand.

Feel totally free to hire me on a Concrete5 project any time though if the need arises :)
jb1 replied on at Permalink Reply
jb1
I've been using C5 for many years, and didn't even realise the bID changed so frequently. That explains a whole lot. If there's anything I can do to help in return just drop me a msg any time. :-)