I'd like a isDirty($bean) function in my code

I find it very useful in my Laravel code and now I kind of need it in my sugar code.  I have many external sources putting data in my system. We have many custom APIs that save data or update data. But sometimes these external systems send Data it is exactly the same as the data that's already in the database, my system does the save anyway which kicks off workflows BPM's logic hooks yada yada yada.  All of this is unnecessary since nothing has really changed. What I'd like to do is before I save anything I'd like to check to see if there are any differences. But there's just no good way to do this, and I'm kind of worried about slowing down the system too much. 

My first attempt is here (I updated it after  answered)

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
/**
* Checks to see if any field in a beam has changed in relation to what's in
* the database right now
*
* It does not check many to many relationships
*
* @param SugarBean $bean
* @return bool
*/
function isDirty(SugarBean $bean): bool
{
$tableName = $bean->table_name;
$beanID = $bean->id;
if (empty($beanID) || empty($tableName)) return false;
$persistedState = is_array($bean->fetched_row)
? array_merge($bean->fetched_row, $bean->fetched_rel_row)
: [];
foreach ($persistedState as $fieldName => $fieldValue) {
if ($fieldName !== 'id_c' && $bean->$fieldName != $fieldValue) {
return true;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

This works well enough for what I need, I'm not sending saves from the UI through it, just calls to my custom APIs so it doesnt affect users at all but still, its not terribly elegant.

Anyone got a better way to do this?

Parents
  • in the Save function of the SugarBean already checks if it's an update. This information is passed along to the logic_hooks in the arguments parameter. I believe it also tells you which fields are changed. 

  • Sure it checks to see if it's an update, but it doesn't check to see if any of the fields are change does it?  I just wanna stop the save if I'm not actually changing data in the database.

  • Check this function in the saveData

    $this->dataChanges = $this->db->getDataChanges($this);
    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    public function getDataChanges(SugarBean &$bean, array $options = [])
    {
    $persistedState = is_array($bean->fetched_row)
    ? array_merge($bean->fetched_row, $bean->fetched_rel_row)
    : [];
    return $this->getStateChanges($bean, $persistedState, $options);
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Reply
  • Check this function in the saveData

    $this->dataChanges = $this->db->getDataChanges($this);
    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    public function getDataChanges(SugarBean &$bean, array $options = [])
    {
    $persistedState = is_array($bean->fetched_row)
    ? array_merge($bean->fetched_row, $bean->fetched_rel_row)
    : [];
    return $this->getStateChanges($bean, $persistedState, $options);
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Children
No Data