我只是希望PHPStorm在使用find(),findAll(),findByAttributes()等时自动完成模型的属性…
我有一个模型:
/** * 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); } ...
当我使用活动记录方法时:
$member = Member::model()->findByAttributes(array('e_mail'=>'Foo Bar'));
$member->
它只给出了CActiveRecord在列表中的参数和方法.
我试图改变
/** * 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()) {...
这个方法从CActiveRecord返回给成员,自己,父,$this,孩子等等…
自动填充仅在“会员”时有效.但是这种方法不仅适用于所有模型,而且只适用于成员模型,因此这不是解决方案.
解决方法
注意:所有我真棒的Yii代码都可以在
bitbucket的存储库
here和
here中免费获得.如果你讨厌Yii的详细程度,请查看我的
Pii课程.我想你会完全挖掘它.
我遇到了这个,我做的是增加静态模型()方法的PHPdoc.这可以解决问题,自动完成工作正常.
例如:
class MyModel extends CActiveRecord { /** * @static * @param string $className * @return MyModel|CActiveRecord */ public static function model($className=__CLASS__) { .... yada yada yada ... } }
请注意管道和附加类添加到“@return”.这告诉PHPStorm还在自动完成查找中包含该类.
另外一个注意事项,如果您使用名称空间,则可能需要在某些类名前面加斜杠.只取决于您的项目和包括.
===============更新时间:2013-08-05 ===============
使用PHPStorm v6及更高版本,看起来您可以使用:
* * @return $this *
并获得适当的自动完成.
另外,整个静态“model()”方法是过时的(如Yii),我有一个基本模型类,我现在用于所有项目.它包含静态模型方法;然后在我的每个子类中不再需要它.这是一个例子……
<?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... } }
自动完成适用于所有子类…
只是想我会更新这个,让你们都知道.