Question

I would like Zend Studio to recognize the class type for all the controller's models, in a CakePHP 2.x project.

This is how I currently do it.

class MyController extends AppController
{
      public $uses = array('Scan');

      public function index()
      {
          /* @var $scan Scan */
          $scan = $this->Scan;
          // do stuff...
      }
}

The above works fine, and Zend will refactor all references to the Scan class correctly, but this is a pain because it has to be done in every action.

I'd like to do this instead.

class MyController extends AppController
{
      public $uses = array('Scan');

      /** @var Scan */
      public $Scan;

      public function index()
      {
          // do stuff...
          $this->Scan->.....
      }
}

When I tried to declare it as a property then $this->Scan is null, and Cake isn't loading the model.

Is there a way to declare it as a property with a phpDoc type without breaking CakePHP?

Was it helpful?

Solution

CakePHP will automatically attach this varialbe to your Controller and what you're essentially doing is redeclaring it.

I am not completely shure how this is done internally in Cake but have you tried declaring $Scan before $uses? Another way would be to redeclare the Controller's constructor to hadnle this but that seems a bit too far-fetched.

Total refactoring capability is something we're still waiting for and it will come with total adoptance of the PSR standards and especially namespaces.

Edit: Here's a nifty (hack) way to add autoComplete for Helpers in Views. This will work for CakePHP >= 2 and only in Views. Also I've used this only in Eclipse but I suppose it should work in any IDE since it is OOP and PHPDoc based.

"All you need to do: is to create a file called "ThisHelper.php" in the app/View directory (not in View/Helper) with the following contents:

App::uses('AppHelper', 'Helper');
/**
 * this Helper
 *
 * @property Html $Html
 * @property Session $Session
 * @property Form $Form
 */
class this extends AppHelper
{
    var $Html;
    var $Session;
    var $Form;

    public function __contruct()
    {
        $this->Html = new HtmlHelper($View);
        $this->Session = new SessionHelper($View);        
        $this->Form = new FormHelper($View);        
    }
}

$this = new this();

To add support for other helpers (Cake core or not) just add them as variables to this class, for example if you want to add PaginatorHelper do:

@property Paginator $Paginator
....
var $Paginator;
.....
public function __contruct()
{
    .............
    $this->Paginator = new PaginatorHelper($View);
}

Probably the best thing to do is to add properties for all CoreHelpers available in CakePHP. Of course it will work with any custom Helpers if you add them. You do not need to include this file in any PHP Script - just add it to the app/View/ directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top