Using SugarBeans outside of Sugar's framework

We need to be able to run PHP code that makes use of Sugar data in scripts that are outside of Sugar - not scheduler tasks or logic hooks or anything. Completely outside of the Sugar world. Based on what I found here I added the following to my script:

if(!defined('sugarEntry')) define('sugarEntry', true);

require_once('data/BeanFactory.php');

That led to a string of error messages and including various other files, and then I started by including the same files in the base index.php from my Sugar installation, but finally I came to an impasse. My error message is:

 

Notice: Undefined index: log in /var/www/html/include/SugarObjects/VardefManager.php on line 1054 Fatal error: Call to a member function debug() on null in /var/www/html/include/SugarObjects/VardefManager.php on line 1054

The code at line 1054 of VardefManager.php is:

$GLOBALS['log']->debug("VardefManager::loadVardef called for module: $module");

I can't find where "log" is being defined in the GLOBALS scope, and anyway, this is starting to look like a lot more than I should have to do to make this work - my current code draft has six require_once statements in it, not to mention several other very hacky details. Is there a better way to do this? Am I missing something?

Sugar 7.8 Professional.

Parents
  • I'm not sure what you mean. The solution in the thread, as far as I can tell, is to use SugarBeans - that's what I'm bending over backward to try to accomplish. If you mean why am I trying to do it outside of Sugar itself - we are a SAAS business, so basically we use Sugar to maintain our customers, and our customers use our portal to do a number of things which we would like to make record of in Sugar. I can of course do direct SQL inserts into the Sugar tables, but I would rather leverage the infrastructure that exists within Sugar to help us maintain data integrity. For example: when I want to insert a record in a table, I would rather let Sugar generate a GUID than generate one myself.

  • Hi Michael Jones

    Have you looked in to the CLI functionality?  If you create a CLI in "InstanceMode" then you should have access to BeanFactory and SugarBeans for that instance.

    The code would be within the Sugar file system but you could execute it from anywhere as long as you have access to the <sugar_instance>/bin/sugarcrm executable.

    Hope it helps.

  • Wow, that's interesting stuff! So in order to perform CRUD-style operations, you would need to add your own custom commands which would then be executed from command line. It kind of feels like more infrastructure than I was hoping to have to deal with, but I could see some situations where it would be perfect. For example, if I for some reason wanted to bypass Sugar's scheduler and run jobs directly from CRON, this would be ideal. I'll have to think through some use cases and see what I can come up with.

    Trying to avoid direct database inserts/deletes, but I don't want to abstract things in a way that is difficult to maintain after the fact.

Reply
  • Wow, that's interesting stuff! So in order to perform CRUD-style operations, you would need to add your own custom commands which would then be executed from command line. It kind of feels like more infrastructure than I was hoping to have to deal with, but I could see some situations where it would be perfect. For example, if I for some reason wanted to bypass Sugar's scheduler and run jobs directly from CRON, this would be ideal. I'll have to think through some use cases and see what I can come up with.

    Trying to avoid direct database inserts/deletes, but I don't want to abstract things in a way that is difficult to maintain after the fact.

Children
No Data