Search code examples
yiiphpstorm

PHPStorm property-read for Yii model relations


How can I do this in PHPStorm so hinting works correctly when calling the model() method?

For instance:

/**
 * @property-read \Stores $store
 */
class Items extends CActiveRecord
{
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function relations()
    {
        return array('store' => array(self::BELONGS_TO, 'Stores', array('store_id' => 'id')),
    }
}

$items = new Items;
$items->store; // PHPStorm type hints this correctly

Items::model()->store; // PHPStorm does NOT type hint correctly.

Model above minimalist for focus of this post.


Solution

  • You just have to 'tell' PhpStorm what this method returns.

        /**
         * @param $className
         * @returns Items
         */
        public static function model( $className = __CLASS__ ) {
            return parent::model( $className );
        }