Sane bean loading using the BeanFactory

bsoremsugar —  May 30, 2012 — 4 Comments

I know there’s quite a few people who have built Sugar modules for loading on On-Demand that have run across these errors during the scanning process…

Invalid usage of a function $parent_type()
Invalid usage of a function $bean_name()

Why do these come up. We disable the ability to have PHP code that calls anonymous functions and classes, as this is a potential way people could get around the security precautions we put in place in disabling several problematic PHP functions. But there are some ways around this for legitimate uses of anonymous calls, and they are much more flexible to boot!

Let’s look at this use case that came up in with the above. My guess is that it’s something like this…

You can see the problematic anonymous class constructor in there. Let’s change to using BeanFactory instead, and squash these three lines into one…

The getBean call not only loads the bean based upon the given module name, but it also accepts an optional second parameter as shown above to load a record by record ID automatically. This makes the first code snippet’s three lines of odd looking code much cleaner.

Note that the BeanFactory is available in all editions of Sugar from 6.3 onwards.

4 responses to Sane bean loading using the BeanFactory

  1. 

    I’m really glad to see this acknowledged and explained. As far as I’ve ever been able to tell, Module Loader and Package Scan documentation does not acknowledge this restriction. I’ve never regarded it as a security issue, but considered it a bug.

    My work around always involved Switch statements, and they were increasingly ugly when there were several Modules involved. I’ll use BeanFactory in the future. Thanks for the example!

  2. 

    This is great, but one small issue. Is there something similar but for retrieve_by_string_fields()?

    Thanks!

Trackbacks and Pingbacks:

  1. SugarCRM Developer Blog » Blog Archive » HOWTO: Using the bean instead of SQL all the time. - June 5, 2012

    […] note that if you are using Sugar 6.3 or later, check out this post for an even easier way to load […]

  2. Populate Custom Module via Results of Custom Job - June 23, 2012

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s