duanben4771 2015-11-05 11:17
浏览 38

Yii活动记录查找功能类提示

I'm stuck using the old yii.
Its active record model can be used as follows:

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();

the problem is that the IDE (PhpStorm) does not allow me to ctrl+click the publish function in $booking->publish() because it does not know that the value returned by findByPk is a Booking instance.

I can fix this like follows

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
}

The problem is that this solution is not a clean one, now I have to define every fetching function find, findAll, findByPk... in every AR model class.

Another way to do it is like this

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

But this has to be defined whenever it's used, which is also bothersome because it's used in many places.

Is there a clean way to this without adding as many method definitions?

  • 写回答

1条回答 默认 最新

  • drsqpko5286 2016-04-20 17:18
    关注

    Time has passed, I found a solution

    The trick is that you can return @return static in the comments, which is exactly what you need for PHPStorm to detect the correct class, so lets create a BaseModel that extends CActiveRecord and has the correct doc comments

    So change class MyClass extends CActiveRecord with class MyClass extends BaseModel

    while using this, now all the class hinting will work :)

    <?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();
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题