Model-based code insight and completion in NetBeans

By Symen Timmermans (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:

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

And this controller:

Controller Class:

Download code <?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:

Download code <?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:

Download code <?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.

Download code
$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

 

Comments 918

CakePHP Team Comments Author Comments
 

Comment

1 Thanks!

As a Netbeans & CakePhp user, you've made my day
Posted Jan 21, 2009 by Mariano Guezuraga
 

Comment

2 Thanks!

Helped a lot for me. Could we get further to enhance and spring to the source of the function or property of a model?
Posted Jan 21, 2009 by Bolla Sandor
 

Comment

3 Helpers completion

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();
?>
Posted Jan 21, 2009 by Matthew Campbell
 

Comment

4 Re: Helpers completion

Thanks Matthew! I got it working.. Now my only gripe with NetBeans is the code completion is a little too slow for my taste...
Posted Jan 22, 2009 by Symen Timmermans
 

Comment

5 Re: Helpers completion

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!
Posted Jan 22, 2009 by Symen Timmermans
 

Comment

6 finally I can have it all

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
Posted Jan 22, 2009 by Stephen Sugden
 

Comment

7 Very Good

But i could not get the completion in a case of a Model relates to another model $this->Model->Model->method

Posted Jan 23, 2009 by Paulo Marcelo
 

Comment

8 Not working

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.
Posted Jan 29, 2009 by Abel Birya
 

Comment

9 Re: Not working

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...
Posted Jan 29, 2009 by Symen Timmermans
 

Comment

10 not working

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.
Posted Jan 29, 2009 by Abel Birya
 

Question

11 more methods?

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.
Posted Feb 13, 2009 by Ryan
 

Comment

12 More methods!

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.
Posted Feb 16, 2009 by Raymond
 

Question

13 is there auto completion for model variables?

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.
Posted Mar 6, 2009 by sajid sharlemin
 

Comment

14 most excellent

thanks for the tip.

together, adding the include path for cake and using Class property variables has made my life a whole lot easier
Posted Apr 11, 2009 by Luke Hoggett
 

Comment

15 can't get it to work!

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();
?>
Posted Jun 23, 2009 by Ziad
 

Comment

16 EasyEclipse

hi, EasyEclipse did the same code completion!..
Posted Jun 26, 2009 by Miguelangel Cabrera
 

Comment

17 Useful tip

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
Posted Oct 23, 2009 by long nguyen
 

Comment

18 Super

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
Posted Nov 13, 2009 by Cesar Sanz