【问题标题】:Yii active record find function class hintingYii活动记录查找功能类提示
【发布时间】:2016-02-06 04:59:29
【问题描述】:

我被旧的 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)不允许我在$booking->publish()中ctrl+click发布功能,因为它不知道findByPk返回的值是一个Booking实例。

我可以像下面这样解决这个问题

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
}

问题是这个解决方案不是一个干净的解决方案,现在我必须在每个 AR 模型类中定义每个获取函数 findfindAllfindByPk...。

另一种方法是这样的

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

但这必须在使用时定义,这也很麻烦,因为它在很多地方都使用过。

有没有一种不添加尽可能多的方法定义的干净方法?

【问题讨论】:

  • 内联变量提示?例如/** @var Booking $booking */$booking = $model->findByPk(815); 行之前(或之后)。
  • 对不起,我补充说它不会在发布问题后 10 秒,您可能没有注意到它

标签: php activerecord yii phpstorm phpdoc


【解决方案1】:

时间过去了,我找到了解决办法

诀窍是您可以在 cmets 中返回 @return static,这正是 PHPStorm 检测正确类所需要的,因此让我们创建一个扩展 CActiveRecord 并具有正确文档 cmets 的 BaseModel /p>

所以把class MyClass extends CActiveRecord改成class 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();
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    相关资源
    最近更新 更多