PhpStorm cannot autocomplete model attributes

I just want PhpStorm to autocomplete my model attributes when I use find (), findAll (), findByAttributes (), etc ...

I have a model like:

/** * member model parameters: * @property integer $id * @property integer $city_id * @property string $e_mail */ class Member extends CActiveRecord { /** * @static * @param string $className * @return Member */ public static function model($className = __CLASS__) { return parent::model($className); } ... 

When I use active recording methods, for example:

 $member = Member::model()->findByAttributes(array('e_mail'=>'Foo Bar')); 

and try autofill when I wrote this:

 $member-> 

It only gives me the CActiveRecord parameters and methods in the list.

I tried to change

  /** * Finds a single active record that has the specified attribute values. * See {@link find()} for detailed explanation about $condition and $params. * @param array $attributes list of attribute values (indexed by attribute names) that the active records should match. * An attribute value can be an array which will be used to generate an IN condition. * @param mixed $condition query condition or criteria. * @param array $params parameters to be bound to an SQL statement. * @return CActiveRecord the record found. Null if none is found. */ public function findByAttributes($attributes,$condition='',$params=array()) {... 

this method returns param from CActiveRecord to Member, self, parent, $ this, child, etc. AutoComplete only worked when it was "Member." But this method is used for all models, not only for the Member model, so this is not a solution.

If anyone knows the solution (preferably without changing the basic framework methods), I will be glad.

+4
source share
2 answers

NOTE All of my awesome Yii code is freely available on bitbucket in the repositories here and here . If you hate the Yii verbosity, check out the Pii class. I think you will dig it out completely.

I came across this, and what I am doing is increasing phpdoc for the static model () method. This fixes the problem and autocomplete works fine.

For instance:

 class MyModel extends CActiveRecord { /** * @static * @param string $className * @return MyModel|CActiveRecord */ public static function model($className=__CLASS__) { .... yada yada yada ... } } 

Pay attention to the channel and additional class added to "@return". This suggests that PhpStorm also includes this class in automatic search termination.

One more note: if you use namespaces, you may need a slash in front of class names. Just depends on your project and includes.

================ UPDATE: 2013-08-05 ================

With PhpStorm v6 and above, it looks like you can use:

  * * @return $this * 

And also get the right auto-complete.

On the side of the note, the entire static thing of the "model ()" method is deprecated (for example, Yii), and I have a base model class that I use now for all projects. It contains a static model method; which is then no longer required in each of my subclasses. Here is an example ...

 <?php namespace My\Awesome\Name\Space; /** * MyBaseModel * etc.etc. */ class MyBaseModel extends \CActiveRecord { /** * Returns the static model of the specified AR class. * * @param string $className * * @return $this */ public static function model( $className = null ) { return parent::model( $className ? : \get_called_class() ); } //code code code code } /** * MySubModel */ class MySubModel extends MyBaseModel { /** * {@InheritDoc} */ public function tableName() { return 'my_sub_table'; } } $_models = MySubModel::model()->findAll( 'xyz = :xyz', array( ':xyz' => 'abc' ) ); if ( !empty( $_models ) ) { foreach ( $_models as $_model ) { // Do awesome stuff... } } 

Autocomplete works fine for all subclasses ...

Just thought I was updating this and letting you know.

+3
source

You can use phpdoc @method . You can use this approach for commonly used models or create a new template for a code generator.

  /** * @method Member findByPk($pk,$condition='',$params=array()) */ class Member extends CActiveRecord { 
0
source

Source: https://habr.com/ru/post/1414534/


All Articles