2015-11-05 42 views
2

我卡住了使用舊的yii。
它的活動記錄模式,可以使用如下:Yii主動記錄查找功能類提示

class Booking extends CActiveRecord { 
    /** 
    * @param string $className 
    * @return Booking 
    */ 
    public static function model($className = __CLASS__) 
    { 
     return parent::model($className); 
    } 
    public function publish(){ 
     // code to publish the booking 
    } 

    // ... the rest of the generated code for the table 

} 

$model = Booking::model(); 
$model->getUnallocated(); 

$booking = $model->findByPk(815); 
$booking->publish(); 

的問題是IDE(PhpStorm)不允許我爲Ctrl +單擊$booking->publish()發佈功能,因爲它不知道返回的值通過findByPk是一個預訂實例。

我可以解決此類似如下

class Booking extends CActiveRecord { 
    /** 
    * @return Booking|null 
    */ 
    public function findByPk($pk,$condition='',$params=array()) 
    { 
     return parent::findByPk($pk,$condition,$params); 
    } 

    // ... the rest of the class 
} 

的問題是,這個解決方案不是一個乾淨的,現在我必須定義每個取回功能findfindAllfindByPk ......在每一個AR模型類。

另一種方式來做到這一點是這樣的

/** @var Booking $booking */ 
$booking = $model->findByPk(815); 

但是,這時候它曾經被定義,這也是麻煩,因爲它在很多地方使用。

有沒有一種乾淨的方式來添加儘可能多的方法定義?

+0

內聯變量提示?例如'/ ** @var預訂$ booking * /'在'$ booking = $ model-> findByPk(815);'行之前(或之後)。 – LazyOne

+0

對不起,我補充說,發佈問題後不會做10秒,你可能沒有注意到它 –

回答

0

時間已經過去了,我找到了解決辦法

的技巧是,你可以在評論返回@return static,這正是你需要什麼PHPStorm檢測到正確類,所以讓我們創建一個擴展CActiveRecord一個BaseModel並且具有正確的文檔註釋在使用本

因此改變class MyClass extends CActiveRecordclass MyClass extends BaseModel

,現在所有的類提示將工作:)

<?php 
/** 
* Extension of the default Yii Active Record class 
* 
* Edit this class to add custom behaviour to all the models used within the application 
* 
*/ 

class BaseModel extends CActiveRecord 
{ 

    /** 
    * Returns the static model of the specified AR class. 
    * Uses the class that called the model() by default 
    * @param string $className active record class name. 
    * @return static The static model class instance 
    */ 
    public static function model($className = null) 
    { 
     return parent::model($className !== null ? $className : get_called_class()); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @param mixed $pk 
    * @param string $condition 
    * @param array $params 
    * @return static 
    */ 
    public function findByPk($pk,$condition='',$params=array()) 
    { 
     return parent::findByPk($pk,$condition,$params); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @param string $condition 
    * @param array $params 
    * @return static 
    */ 
    public function find($condition='',$params=array()) 
    { 
     return parent::find($condition,$params); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @param string $condition 
    * @param array $params 
    * @return static[] 
    */ 
    public function findAll($condition='',$params=array()) 
    { 
     return parent::findAll($condition,$params); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @param $pk 
    * @param string $condition 
    * @param array $params 
    * @return static[] 
    */ 
    public function findAllByPk($pk,$condition='',$params=array()) 
    { 
     return parent::findAllByPk($pk,$condition,$params); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @param $attributes 
    * @param string $condition 
    * @param array $params 
    * @return static 
    */ 
    public function findByAttributes($attributes,$condition='',$params=array()) 
    { 
     return parent::findByAttributes($attributes,$condition,$params); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @param $attributes 
    * @param string $condition 
    * @param array $params 
    * @return static[] 
    */ 
    public function findAllByAttributes($attributes,$condition='',$params=array()) 
    { 
     return parent::findAllByAttributes($attributes,$condition,$params); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @param $sql 
    * @param array $params 
    * @return static 
    */ 
    public function findBySql($sql,$params=array()) 
    { 
     return parent::findBySql($sql,$params); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @param $sql 
    * @param array $params 
    * @return static[] 
    */ 
    public function findAllBySql($sql,$params=array()) 
    { 
     return parent::findAllBySql($sql,$params); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * This will add a couple of milliseconds with every `with` usage 
    * @return static 
    */ 
    public function with() 
    { 
     return call_user_func_array('parent::with', func_get_args()); 
    } 

    /** 
    * This function exists only to help the IDE autodetect the returned class 
    * @return static 
    */ 
    public function together() 
    { 
     return parent::together(); 
    } 

}