Alan Storm Alan Storm - 8 days ago 8
PHP Question

SugarCRM: Direct Bean Property Access vs. getFieldValue

In modern versions of SugarCRM, after you load data into a bean object

$account = BeanFactory::getBean('Accounts')
->retrieve_by_string_fields(array('name'=>'Q.R.&E. Corp'));


You can fetch data either by directly accessing the property

$account->name


or by using
getFieldValue


$account->getFieldValue('industry')


Is there any historical reason that
getFieldValue
exists? Is there a canonical way of deciding when to use
getFieldValue
vs the direct object property method?

Answer

The only difference I'm seeing is that it will not throw PHP errors if the field doesn't exist, and if it's a boolean value it will convert it to an integer. See the function definition...

function getFieldValue($name)
{
    if (!isset($this->$name)){
        return FALSE;
    }
    if($this->$name === TRUE){
        return 1;
    }
    if($this->$name === FALSE){
        return 0;
    }
    return $this->$name;
}