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.
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.
Download code
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:
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
Comment
1 Thanks!
Comment
2 Thanks!
Comment
3 Helpers completion
<?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();
?>
Comment
4 Re: Helpers completion
Comment
5 Re: Helpers completion
The helper code completion gave me also the Helper parent class methods and properties I never knew about!
Comment
6 finally I can have it all
Could not be happier about this
Comment
7 Very Good
Comment
8 Not working
Kindly assist.
Comment
9 Re: Not working
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...
Comment
10 not working
Question
11 more methods?
Comment
12 More methods!
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.
Question
13 is there auto completion for model variables?
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.
Comment
14 most excellent
together, adding the include path for cake and using Class property variables has made my life a whole lot easier
Comment
15 can't get it to work!
$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
Comment
16 EasyEclipse
Comment
17 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
Comment
18 Super
Thank you netbeans!!!
Regards