5 Replies Last post: Feb 2, 2013 4:31 AM by Andriy Bazanov  
Dillon Gilmore Newbie 6 posts since
Feb 2, 2013
Currently Being Moderated

Feb 2, 2013 2:43 AM

Pre-defining Class Variables in PHP (Yii Models)

I was coding in PHP, using the Yii framework. The columns in Yii turn into Model variables. These variables are automatically created by Yii so I don't think I should need to create them. I do not get any errors or warnings on the site when I run the app without the variables pre defined. I have my framework on PHPStorm set to Yii so I was wondering if someone could get me some answers. I apologize if this question has been asked already.

 

Here is an example. Lets say I have a table in my database.

 

Form

------------------------------------

id  | type  | width | height |

------------------------------------

1  | 'input' | 300   | 200    |

------------------------------------

 

And a Model representing that table.

 

class Form extends CActiveRecord {

}

 

In my controller I am going to ask for those variables in the database based off of ID.

 

class FormController extend CController {

  public function actionIndex() {

    $form_model = new Form;

    $form = $form->findByPK(1);

    $form->id; // PHPStorm warning

    $form->type; // PHPStorm warning

    $form->width; // PHPStorm warning

    $form->height; // PHPStorm warning

  }

}

 

In PHPStorm I get warnings as defined in the comments. The warning is that the variables are not defined. So when I go back to my model and add them to the global scope.

 

class Form extends CActiveRecord {

  public $id;

  public $type;

  public $width;

  public $height;

}

 

The warnings fade. I feel like those variables are already created by Yii. but PHPStorm doesn't know that. I am also connected directly to my database so PHPStorm should know that the models and tables all match up. If someone could tell me why this behavior is occurring. Thanks and sorry again if this has already been answered.

Andriy Bazanov Master 4,158 posts since
May 28, 2010
Currently Being Moderated
Feb 2, 2013 3:51 AM in response to: Dillon Gilmore
Re: Pre-defining Class Variables in PHP (Yii Models)

Hi Dillon,

 

You need to use @property PHPDoc declaration for your class: http://www.phpdoc.org/docs/latest/for-users/phpdoc/tags/property.html

Andriy Bazanov Master 4,158 posts since
May 28, 2010
Currently Being Moderated
Feb 2, 2013 4:33 AM in response to: Dillon Gilmore
Re: Pre-defining Class Variables in PHP (Yii Models)
So I do that for each variable? I think I will request that to be automatically added,

Good luck with waiting. You see -- Yii is not used by 99% of PHP developers, there are plenty other popular frameworks around -- every each of them wants to be "that special" that is supported by that specific / any IDE. Unfortunately, PhpStorm is not at that level yet to concentrate developers resources on support for specific framework only -- much better if there will be something that will suit any framework / most frameworks. And magic functionality requires IDE to know about how that specific framework works; IDE needs to provide some quite tight integration in case...

 

I think it wouldn't be too hard to add the functionality.

I think the same -- it would not be too hard to add field description manually (actually, half manually, as IDE can offer a hand here with creating such fields / @property entries -- just try Alt+Enter on problematic place).

 

Or the error messages should suggest that to add a magic property in a phpdoc.

Alt+Enter (Quick Fix intentions) may help here.

 

 

Alternative approach is to disable (or lower severity) corresponding inspection (Settings | Inspections | PHP | Undefined | Undefined field) -- no warning messages, no worries -- right? Of course -- it's a radical measure which should not be really used (but for some people it was an accetable solution).

Andriy Bazanov Master 4,158 posts since
May 28, 2010
Currently Being Moderated
Feb 2, 2013 5:28 AM in response to: Dillon Gilmore
Re: Pre-defining Class Variables in PHP (Yii Models)

Unfortunately it is the current state of things -- you cannot add such information into existing project quickly (I mean -- in few clicks only).

 

Based on status of current popular tickets that have quite a few votes ... I do not expect to see that deep Yii (or any other) framework  integration in upcoming version. There should be some support for Symfony/Yii (accordingly to their roadmap) but nothing major: http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Development+Roadmap. Some of those tickets have "In Progress" status for quite a while or got scheduled from version to version .. but no great progress has been made (that's why I said "Good luck with waiting").

 

No doubts -- support for popular framework is always a good thing -- it makes a lot of people happier and attracts new users -- you do not have to think about soma basic things (as it managed behind the scene somehow) .. and some complex things are made simple (like the one in your case). Unfortunately such support is not currently here yet .. and therefore it needs to be done in more "traditional" / framework-independent way (PHPDoc tags in this case).

 

Without such deep integration (when IDE knows how framework works)  .. I do not see what you can do here to have such fields be recognized by IDE quickly. One thought though -- you can speedup such task quite a bit by making some custom script that will generate such PHPDoc instructions which you can later just copy-paste. If you write (or find existing one) something like this now (spend a day .. or maybe just a few hours), you can later reuse it in another project. It's definitely better that doing it manually ... or not doing it at all.

More Like This

  • Retrieving data ...