Model-based code insight and completion in NetBeans
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:
And this controller:
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':
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:
This allows me to get code completion and code hinting in the Posts controller for the Author model.
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
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

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
Netbean + PHP + CakePHP are cool
/**
* @property Post $Post
*/
differs than
/*
* @property Post $Post
*/
starting The comment with /** made it workable!
Thank you
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.
<?php
/**
* Description of IndexController
*
* @author aruna
*/
class IndexController extends AppController {
/**
* @var XmlHelper
*/
public $xml;
public function indexAction(){
$this->xml->header();
echo 'hello world';
}
}
?>
class AppHelper extends Helper {
public $Xml;
...
function __construct() {
$this->Xml = new XmlHelper();
...
}
}
in order to use the new helper syntax
Thank you netbeans!!!
Regards
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
together, adding the include path for cake and using Class property variables has made my life a whole lot easier
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.
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.
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...
Could not be happier about this
<?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
$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
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?