快速调用数据库的方法? [ 2.0 版本 ]
class TeacherModel extends \yii\db\ActiveRecord
{
    private $connection;
    public  static function getConnection()
    {   
        //$connection = \yii::$app->db;
        return $this->connection = \yii::$app->db;
    }
    public function getcourse()
    {
        $sql=<<<EOF
            select course_name,id from course
        EOF;
        //$connection=\yii::$app->db;   我不想用这种方法加载,想用类内的构造函数,但是不知道怎么写啊
        $command = $this->connection->createCommand($sql);
        $results = $command->queryAll();
        return $results;
    }
}
想用类内的构造函数,但是不知道怎么写啊
                苹果喽 补充于 2016-08-02 11:30
                
                                    
            
            这个界定符 我也是醉了
共 3 个回答
- 
zhoupenghui 回答于 2016-08-02 11:44 举报public static function (){ $rtn=Course::find()->select(['course_name','id'])->all(); return $rtn; }共 1 条回复 - 
 - 
benbenhu007 回答于 2016-08-03 11:54 举报* $locator = new \yii\di\ServiceLocator; * $locator->setComponents([ * 'db' => [ * 'class' => 'yii\db\Connection', * 'dsn' => 'sqlite:path/to/file.db', * ], * 'cache' => [ * 'class' => 'yii\caching\DbCache', * 'db' => 'db', * ], * ]); * * $db = $locator->get('db'); // or $locator->dbyii2默认的db连接是读取的数据库配置 如果不想用的话 你可以写原生的mysql_connect() 但是感觉没必要 它自带的就很好用
 
苹果喽
            注册时间:2016-10-31
最后登录:2022-03-13
在线时长:6小时10分
    最后登录:2022-03-13
在线时长:6小时10分
- 粉丝9
 - 金钱2825
 - 威望10
 - 积分2985