TYPO3 extbase, get modified and previous value of the object

Recently I had a project, where I had to make a log of every actions like create, update, delete. In this case of update, I need to fetch previous and after update values.

So the output I need was like below


Object XYZ have been update by user ABC
Old value is "Test"
New value is "Test 1"

So to achieve this I have wrote a service and call that service in updateAction() unction.

Service

LogService.php

class LogService implements \TYPO3\CMS\Core\SingletonInterface {

    /**
     * @param object $object
     */

    function writeLog($object){
        $fieldsToCheck = array('title','description');
        //iterate through $fieldsToCheck
        foreach($fieldsToCheck as $fieldsCheck){
            //_isDirty: only if field updated
            if($object->_isDirty($fieldsCheck)){
                $description .='<b>Old Value:</b>:';
                $description .= $object->_getCleanProperty($fieldsCheck).'<br/>';
                $description .='<b>New Value:</b>:';
                $description .= $object->_getProperty($fieldsCheck).'<hr/>';
            }
        }
    }

    //Insert log to Log db
    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_myext_domain_model_logs');
    $queryBuilder
        ->insert('tx_myext_domain_model_logs')
        ->values([
            'description' => $description,
            'user' => $GLOBALS['TSFE']->fe_user->user['uid'],
            'ipaddress' => $_SERVER['REMOTE_ADDR'],
            'crdate' => time(),
        ])
        ->execute();
}



Controller

MyextController.php

  /**
     * action update
     *
     * @param \KVT\Myext\Domain\Model\Something $something
     * @return void
     */
    public function updateAction(\KVT\Myext\Domain\Model\Something $something)
    {
        $this->addFlashMessage('Item updated', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::OK);
        $this->somethingRepository->update($something);
       
      
       //log service
        $this->logService->writeLog(
            $something
        );
    }