Wednesday, January 1, 2020

PHPStorm and Code Completion with BeanFactory

PHPStorm and Code Completion with BeanFactory

I have been using PHPStorm with SugarCRM for most of a decade now and it is great.  For writing PHP code it is at the top of the heap.  The one place where I have a problem is with code completion with SugarCRM and BeanFactory.  This video shows what I am talking about.



Without the ability to complete field and relationship names I am left to remember them all (not going to happen) or seek them out and copy-n-paste.  Neither are a great way to be productive.  I have been looking for a solution for years now and I finally stumbled upon one that works OK. 

I tried all sorts of "GroovyScripts" but I could never get it to work right, and I am no where near ready to start writing Plugins for PHPStorm yet so this was my workaround.

The script below adds a "Live Template" for every module in SugarCRM to PHPStorm.  It processes the $bean->fieldDefs to get all the names for fields and relationships.  Live templates are available in PHPStorms preferences panel.


The script does this by creating (or overwriting if it already exists, so beware) the Live Template configuration file for PHP (/Users/USER_NAME/Library/Preferences/PhpStorm2019.3/jba_config/templates/PHP.xml on my Mac).  I am sure over the next few days I will tweak this a bit but if you have any suggestions I would love to hear them.  Its already a thousand times better than what I was doing before and I think as far as look and feel its as good as I could do with the Live Template tool but maybe I will tweak the abbreviation or what I am including from the fieldDefs, not sure.

So, Here is the script.  I added it to my SugarCRM as an Administration Menu option.  Look here for instructions to do that.  After you run it you have to restart PHPStorm so it reads the new XML file.  On my system apache runs under my user so permissions are not an issue.  If your apache runs under a different user then you might have to have the script create the XML in your home directory and then manually copy it to the right place.


No comments:

Post a Comment