Getting block updated date or added date doesn't work1 user found helpful
There is a method for the Block object, so that's not the issue:
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:
I'm hoping I'm not the only one who is in this situation. Does anyone have a workaround?
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.
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?
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>';
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?
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 :)