为了在应用程序中使用我们建立的blog数据库,我们需要修改保存在/wwwroot/blog/protected/config/main.php的配置文件。该配置文件是一个php脚本文件,返回组合名值对的数组。其中的每一项都是用户初始应用实例的属性。

我们配置应用的components属性,给它添加一个名字为db的新的入口,参考如下

如果使用的是mysql数据库,可参考如下配置
return array(
     ......
     'components'=>array(
         ......
         'db'=>array(
              'connectionString'=>'mysql:host=localhost;dbname=yii',
              'username'=>'root',
              'password'=>'',
              'charset'=>'UTF8'
          ),
      ),
      ......
);
return array(
    ......
    'components'=>array(
        ......
        'db'=>array(
            'connectionString'=>'mysql:host=localhost;dbname=yii',
        'username'=>'root',
        'password'=>'',
        'charset'=>'UTF8'
        ),
    ),
    ......
);

上面的配置表示我们有一个连接导数据的组件。可以在程序的任何地方通过使用 Yii::app()->db 来获取连接到数据的对象。Yii::app()用来返回我们在入口程序中建立的应用对象。大多数情况下我们不会直接使用数据库连接,而是使用所谓的 ActiveRecord来和数据交互。

8 Comments

  1. I am impressed, I must say. Very seldom do I come across a blog that’s both educational and entertaining, and let me tell you, you’ve hit the nail on the head. Your opinion is outstanding; the issue is something that not a lot of people are speaking intelligently about. I am really happy that I stumbled across this in my search for something relating to it.

  2. The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.

  3. Sorry for the huge review, but I’m really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it’s the right choice for you.

  4. I am usually to blogging and i actually respect your content. The article has actually peaks my interest. I am going to bookmark your web site and hold checking for brand spanking new information.

  5. Many thanks for your time and effort to have put these things together on this blog site. Michael and I very much liked your knowledge through the articles with certain things. I understand that you have many demands with your schedule hence the fact that a person like you took all the time just like you did to help people like us by means of this article is also highly loved.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


*