phpsymfonyobjectentitydifference

Symfony comparing two entities and getting names of different fields


Is there an easy way of comparing two entities and getting the names of the fields which do not match without using an if statement for each of the them?


Solution

  • $foo = new Foo();
    $bar = new Bar();
    
    $array_diff = array_keys(    
        array_diff_key(
            get_object_vars($foo),
            get_object_vars($bar)
    ));
    

    $array_diff will be an array containing every property of $foo that is not found in $bar.

    I'm from my smartphone so I didn't tested it