PHP code example of chinaphp / yii2-ide-helper

1. Go to this page and download the library: Download chinaphp/yii2-ide-helper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chinaphp / yii2-ide-helper example snippets




$config = [
    'controllerMap' => [
        'ide-helper' => 'Chinaphp\Yii2IdeHelper\Console\Controller',
    ],
    'components' => [
        'ide-helper' => [
            'class' => 'Chinaphp\Yii2IdeHelper\Config\ConfigProvider',
            'output_dir' => dirname(__DIR__),
            'filename' => '_ide_helper.php',
            'meta_filename' => '.phpstorm.meta.php',
            'config_paths' => [
                '@app/config/web.php',
                '@app/config/console.php',
            ],
        ],
    ],
    ],
];

override(\Yii::$app->get('db'), type(\yii\db\Connection));

$db = Yii::$app->get('db');
$result = $db->createCommand('SELECT * FROM users')->queryAll(); // 有完整的类型提示

override(\Yii::$app->db, type(\yii\db\Connection));

$result = Yii::$app->db->createCommand('SELECT * FROM users')->queryAll(); // 有完整的类型提示

override(\yii\db\ActiveRecord::find(), type(\yii\db\ActiveQuery));

$query = User::find(); // PhpStorm 知道返回的是 ActiveQuery
$query->where(['status' => 1]); // 有完整的类型提示

$generator = new MetaGenerator($config);
$generator->addBinding('custom', ['Custom\Class']);
$generator->save();

// console/config/main.php
'components' => [
    'ide-helper' => [
        'class' => 'Chinaphp\Yii2IdeHelper\Config\ConfigProvider',
        'output_dir' => dirname(__DIR__),
        'filename' => '_ide_helper',
        'meta_filename' => '.phpstorm.meta.php',
        'config_paths' => [
            '@app/config/web.php',
            '@app/config/console.php',
        ],
    ],
],

namespace Yii {
    class App {
        public static $app;
    }
}

namespace {
    class Yii extends \Yii\BaseYii {
        /**
         * @var \yii\db\Connection
         */
        public $db;
        
        /**
         * @var \yii\caching\Cache
         */
        public $cache;
    }
}

namespace app\models {
    /**
     * @property int $id
     * @property string $title
     * @property-read \app\models\User $user
     * @property-read \app\models\Comment[] $comments
     */
    class Post extends \yii\db\ActiveRecord {
    }
}


namespace PHPSTORM_META {
    // DI 容器绑定
    override(\Yii::$app->get('db'), type(\yii\db\Connection));
    override(\Yii::$app->db, type(\yii\db\Connection));

    override(\Yii::$app->get('cache'), type(\yii\caching\FileCache));
    override(\Yii::$app->cache, type(\yii\caching\FileCache));

    // ActiveRecord 类型推断
    override(\yii\db\ActiveRecord::find(), type(\yii\db\ActiveQuery));
    override(\yii\db\ActiveRecord::hasMany(), type(\yii\db\ActiveQuery));
    override(\yii\db\ActiveRecord::hasOne(), type(\yii\db\ActiveQuery));
}
bash
php yii ide-helper/generate
bash
php yii ide-helper/models
bash
php yii ide-helper/meta
bash
php yii help