Model-based code insight and completion in NetBeans

by SymenTimmermans
As a lazy programmer I really like code completion and code insight. It's the only thing I do like about Visual Studio. It somehow knows all your classes, the ones they extend, all functions and properties and their types...etc. PHP developers have also had this functionality for a few years now, with Aptana, PHPEclipse or Zend Studio (I know there's more). Unfortunately CakePHP binds models to controllers in a way IDE's don't understand, without helping them a little. Let me show you how to approach this in NetBeans.
I started using Netbeans (http://www.netbeans.org/features/php/) a while ago, because I was sick of the big footprint eclipse-based IDE's have. Aptana was slowing me down.

So I started using NetBeans PHP and discovered that it has some great features.

The only thing it doesn't do is complete your code in the controller with model-based methods and properties. On the blog for Netbeans PHP I yakked a bit about this (http://blogs.sun.com/netbeansphp/entry/defining_a_variable_type_in#comment-1232115150000) and was directed to this screencast: http://blogs.sun.com/netbeansphp/entry/screencat_about_class_property_variables. Petr tells us how to achieve the wanted functionality (he did forget a '$' in the definition, but hey, we all make mistakes).

We can make this work for Cake. Let's say we have the following model:

Model Class:

<?php 
class Post extends AppModel {
    var 
$name 'Post';
    var 
$belongsTo = array('Author' => array('className' => 'Author''foreignKey' => 'author_id'));
}
?>

And this controller:

Controller Class:

<?php 
class PostsController extends AppController {
    var 
$name 'Posts';
    function 
index() {
        
$posts $this->Post->find('all');
        
$this->set('posts'$posts);
    }
}
?>

When you type " $this->Post-> ", normally, you will not get a list of functions we can call for the Post model or the parent AppModel.
We do want this.

We can do this by 'helping' NetBeans a little with 'Class property variables':

Controller Class:

<?php 
/**
 * @property Post $Post
 */
class PostsController extends AppController {
    var 
$name 'Posts';
    function 
index() {
        
$posts $this->Post->find('all');
        
$this->set('posts'$posts);
    }
}
?>

See the new comment above the PostsController? This is telling NetBeans our class has a property $Post with class Post (upon typing, NetBeans automatically lists all classes found in your code base, along with the filename where it is defined), which is used for code completion and code hinting.

This works for controllers, but even better, it also works for models, so you can link all relations in your model to the relevant model class. If I would do this to the model in my example, you'd get this:


Model Class:

<?php 
/**
 * @property Author $Author
 */
class Post extends AppModel {
    var 
$name 'Post';
    var 
$belongsTo = array('Author' => array('className' => 'Author''foreignKey' => 'author_id'));
}
?>

This allows me to get code completion and code hinting in the Posts controller for the Author model.


$this->Post->Author->...

And NetBeans would provide me with a list of all methods and properties of the Author model and of course it's parent, AppModel.

You can go as deep as you want.
Check this screenshot of the app I'm working on:

http://livesurvey.nl/files/NetBeansModelCompletion.jpg
I hope I have been clear enough... otherwise, let me know in the comments.

I don't have enough experience with other IDE's to be able to tell you if such a thing is possible in your preferred IDE, but I wouldn't be surprised! In any way, discovering this has made my life a little easier, so I thought I'd share!

Have Fun,

Symen Timmermans

Report

More on Tutorials

Advertising

Comments

  • Tapan posted on 01/23/11 03:32:48 AM
    Hello All,

    I have gone through to various blogs regarding auto completion with Netbeans and finally it is working.

    You need to include complete cakephp folder in your application and set below mentioned code before your controller code starts and it works.

    /**
    * @property Post $Post
    */

    Example:
    set('posts', $this->Post->find('all'));
    }
    }

    ?>
    Regards
    Tapan Thapa
  • amykhar posted on 12/29/10 08:53:08 AM
    Thanks for the tip.
  • netresource posted on 11/16/10 10:04:36 PM
    Greate aritcle.
    Netbean + PHP + CakePHP are cool
  • saidbakr posted on 10/17/10 07:23:14 AM
    Sorry, I just commented telling that hint does not work. I did not realized that
    /**
    * @property Post $Post
    */
    differs than
    /*
    * @property Post $Post
    */

    starting The comment with /** made it workable!

    Thank you
  • saidbakr posted on 10/17/10 07:10:13 AM
    The hint does not work with NetBeans 6.9.1. I use it under Ubuntu 10.04.
  • euromark posted on 09/20/10 02:18:13 PM
    You could modify my code completion shell script
    I designed it to work with PHPDesigner:
    http://www.dereuromark.de/2010/06/28/code-completion-console-script/
    But it might as well work for other IDEs as well.
  • harshadsm posted on 09/05/10 11:27:59 PM
    How to get code completion working for Components like Auth, Acl, etc. ?
  • faster82 posted on 06/12/10 02:27:37 PM
    I tried following way and it worked.

    <?php
    /**
     * Description of IndexController
     *
     * @author aruna
     */
    class IndexController extends AppController {
        
    /**
         * @var XmlHelper
         */
        
    public $xml;
        public function 
    indexAction(){
            
    $this->xml->header();
            echo 
    'hello world';
        }
    }
    ?>
  • euromark posted on 06/05/10 05:26:37 AM
    but there it is now (for cake1.3)

    class AppHelper extends Helper {

    public $Xml;
    ...

    function __construct() {
    $this->Xml = new XmlHelper();
    ...
    }

    }

    in order to use the new helper syntax
  • snake1024 posted on 02/20/10 05:54:14 AM
    Finally using my favorite PHP IDE with cakePHP
  • spiralni posted on 11/13/09 05:12:43 PM
    Hi, I've always used NetBeans for my developments, and recently I started to play with CakePHP... I was using notepad for editing and it is a pain in the ass, but now, with this netbeans support I must to say..

    Thank you netbeans!!!

    Regards
  • loga posted on 10/23/09 04:17:32 AM
    Thanks for this useful tip.
    if you want to get auto completion from $this property, you can do like this:
    From Include Path right click and choose Property.
    Choose Add Folder and select folder where contains cake/libs
  • miguel4ngel posted on 06/26/09 01:17:58 PM
    hi, EasyEclipse did the same code completion!..
  • artefact posted on 04/11/09 09:17:08 PM
    thanks for the tip.

    together, adding the include path for cake and using Class property variables has made my life a whole lot easier
  • sajid_bd posted on 03/06/09 10:48:41 AM
    I am new to cakephp .I followed your guide.i get all function auto completion but no property . After typing
    var i don't get any auto completion for property.
    for example in your code-

    class Post extends AppModel {
    var $name = 'Post';
    var $belongsTo = array('Author' => array('className' => 'Author', 'foreignKey' => 'author_id'));
    }
    ?>
    i expect auto completion for $name and $belongsTo.But i get No Suggestion.

    Any Help? Thanks in advance.
  • rnickel posted on 02/13/09 01:32:40 PM
    Any way for it to show the find() findAll() and all the other methods relating to the model? Right now it only shows me all the methods I've created in my model.
    • blavla posted on 02/16/09 08:17:51 AM
      Any way for it to show the find() findAll() and all the other methods relating to the model? Right now it only shows me all the methods I've created in my model.
      Yes you can:
      Right-click on the project -> PHP Include Path and add your cake-folder. Now you also should see all the build-in-functions.
  • asbrname posted on 01/29/09 07:59:52 AM
    Hey, I'm a bit new to cakePHP but I'm used to using Netbeans to work with in java. I have tried adding the properties as you have indicated above but I'm still not getting the results.
    Kindly assist.
    • SymenTimmermans posted on 01/29/09 08:07:22 AM
      Hey, I'm a bit new to cakePHP but I'm used to using Netbeans to work with in java. I have tried adding the properties as you have indicated above but I'm still not getting the results.
      Kindly assist.

      Hi Abel. I'm sorry it is not working for you (yet). First off:

      Are you using Netbeans for PHP, version 6.5 ?

      Please post the top part of your controller or model, the one containing your @property tags... Use the BBcode tags to your advantage...
      • asbrname posted on 01/29/09 08:26:24 AM
        Actually no. Im using Ubuntu 8.04 and the version on the repositories is 6.1 but I have the original DVD from Neatbeans and I can install it right now if need be. If this copy will not work also, where can I get a copy of Netbeans for PHP coz I just installed the PHP modules in the one I had.
  • paulomarcelo posted on 01/23/09 12:16:00 PM
    But i could not get the completion in a case of a Model relates to another model $this->Model->Model->method

  • grncdr posted on 01/22/09 02:08:33 PM
    Thanks so much for this tip, it caused me to take a look at NetBeans, and specifically to find the jVi (vim editing modes) plugin for NetBeans. I now have vim editing, model-introspection and excellent code completing. I've been a stolid vim/gvim user for years, but this might finally bring me into the IDE age...

    Could not be happier about this
  • SymenTimmermans posted on 01/22/09 08:19:43 AM
    Thanks Matthew! I got it working.. Now my only gripe with NetBeans is the code completion is a little too slow for my taste...
  • mattPhp posted on 01/21/09 11:13:42 PM
    You can also get helpers auto completion in Netbeans. Create a new folder and paste the following code into a file within it. Then select the projects window, right click "Include Path", select properties, and add the folder.

    <?php
    exit();
    $ajax = new AjaxHelper();
    $form = new FormHelper();
    $html = new HtmlHelper();
    $javascript = new JavascriptHelper();
    $number = new NumberHelper();
    $session = new SessionHelper();
    $text = new TextHelper();
    $time = new TimeHelper();
    $pagination = new PaginationHelper();
    $rss = new RssHelper();
    $xml = new XmlHelper();
    ?>
    • Dealerpriest posted on 04/04/10 12:03:11 PM
      You can also get helpers auto completion in Netbeans. Create a new folder and paste the following code into a file within it. Then select the projects window, right click "Include Path", select properties, and add the folder.

      <?php
      exit();
      $ajax = new AjaxHelper();
      $form = new FormHelper();
      $html = new HtmlHelper();
      $javascript = new JavascriptHelper();
      $number = new NumberHelper();
      $session = new SessionHelper();
      $text = new TextHelper();
      $time = new TimeHelper();
      $pagination = new PaginationHelper();
      $rss = new RssHelper();
      $xml = new XmlHelper();
      ?>

      Apparantly this no longer works in NB 6.8
      http://netbeans.org/bugzilla/show_bug.cgi?id=180239
    • ziad posted on 06/23/09 01:04:25 PM
      I tried doing this for another project (a non CakePHP project) but couldn't get it to work. Any ideas why? I did exactly as you said, I created a file in an include folder where I've got things like this:


      $agentDAO = new AgentDAO();

      And then in another place I try and do code completion on a variable called $agentDAO but it doesn't give me anything.

      Thanks
      Ziad

      You can also get helpers auto completion in Netbeans. Create a new folder and paste the following code into a file within it. Then select the projects window, right click "Include Path", select properties, and add the folder.

      <?php
      exit();
      $ajax = new AjaxHelper();
      $form = new FormHelper();
      $html = new HtmlHelper();
      $javascript = new JavascriptHelper();
      $number = new NumberHelper();
      $session = new SessionHelper();
      $text = new TextHelper();
      $time = new TimeHelper();
      $pagination = new PaginationHelper();
      $rss = new RssHelper();
      $xml = new XmlHelper();
      ?>
    • SymenTimmermans posted on 01/22/09 08:26:19 AM
      By the way, I love how Netbeans' code completion exposes the entire Cake API with docs.
      The helper code completion gave me also the Helper parent class methods and properties I never knew about!
      • Dealerpriest posted on 04/02/10 07:58:18 PM
        By the way, I love how Netbeans' code completion exposes the entire Cake API with docs.
        The helper code completion gave me also the Helper parent class methods and properties I never knew about!

        Exactly how do you make it to show the docs? Should I download the api and place it somewhere?
  • carstep posted on 01/21/09 09:25:53 AM
    Helped a lot for me. Could we get further to enhance and spring to the source of the function or property of a model?
  • mguezuraga posted on 01/21/09 09:03:01 AM
    As a Netbeans & CakePhp user, you've made my day
login to post a comment.