当前位置:首页 > 芯闻号 > 充电吧
[导读]1.yii中与数据库连接的时候,是通过ActiveRecord进行连接的,一般需要与数据库表进行对应的类需要继承ActiveRecord,而对于该表中数据库的查询,同样也是在该 User类中定义的查询

1.yii中与数据库连接的时候,是通过ActiveRecord进行连接的,一般需要与数据库表进行对应的类需要继承ActiveRecord,而对于该表中数据库的查询,同样也是在该 User类中定义的查询方法,不同的是,该查询的方法定义是static的。
2. 对于yii与数据库的链接是在配置文件中已经描写过的


run();

3.而对应的common/config/main-local.php文件内容是这样的:


 [
        'db' => [
            'class' => 'yiidbConnection',
            'dsn' => 'mysql:host=10.1.20.36;dbname=db_XXX',
            'username' => 'weihu_dev',
            'password' => 'xxxxxx',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yiiswiftmailerMailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
];

4.整个初始化Yii::$app->db的初始化过程是这样的,整个配置字符是在main-local.php当中的,而其内容是在$config中的,这个过程是在base/Application中的

Component::__construct($config);最终实现的是Yii::configure其内容是:
public static function configure($object, $properties)
{  
    foreach ($properties as $name => $value) {

        $object->$name = $value;
    }
    return $object;
}


$object的实参是$app而其中有一个参数是component,而对component的参数的设置,还有一个真正的函数覆盖,setComponent,该函数的定义是在ServiceLocator,因为Application的父类是Module而Module的父类则是ServiceLocator
该类中定义了
public function setComponents($components)
{
    foreach ($components as $id => $component) {
        $this->set($id, $component);
    }
}
其set的定义是:
public function set($id, $definition)
{
    if ($definition === null) {
        unset($this->_components[$id], $this->_definitions[$id]);
        return;
    }

    unset($this->_components[$id]);

    if (is_object($definition) || is_callable($definition, true)) {
        // an object, a class name, or a PHP callable
        $this->_definitions[$id] = $definition;
    } elseif (is_array($definition)) {
        // a configuration array
        if (isset($definition['class'])) {
            $this->_definitions[$id] = $definition;
        } else {
            throw new InvalidConfigException("The configuration for the "$id" component must contain a "class" element.");
        }
    } else {
        throw new InvalidConfigException("Unexpected configuration type for the "$id" component: " . gettype($definition));
    }
}


这样我们就知道会有一个$id是"db"  而$definition则是,具体db的数组
'db' => [
    'class' => 'yiidbConnection',
    'dsn' => 'mysql:host=db1-dev.bj1.haodf.net;dbname=db_Hdf',
    'username' => 'weihu_dev',
    'password' => 'hdf@haodf.com',
    'charset' => 'utf8',
]



这样db的配置就放在了$_definitions 当中,当我们不使用的时候,存放的就是数组,当我们使用的时候,则会调用ServiceLocator的get函数。
当我们调用Yii::$app->db的时候出发 base/application中的
public function getDb()
{
    return $this->get('db');
}


函数,而get函数则会调用父类的get函数
public function get($id, $throwException = true)
{

    if (isset($this->_components[$id])) {
        return $this->_components[$id];
    }

    if (isset($this->_definitions[$id])) {
        $definition = $this->_definitions[$id];
        if (is_object($definition) && !$definition instanceof Closure) {
            return $this->_components[$id] = $definition;
        } else {
            return $this->_components[$id] = Yii::createObject($definition);
        }
    } elseif ($throwException) {
        throw new InvalidConfigException("Unknown component ID: $id");
    } else {
        return null;
    }
}



之前将数据库的配置放在了$_definition当中,那么此时会判断如果  $definition 是数组,那么就会创建对应的对象Yii::createObject($definition);
至此就创建了该对象!!!可以与数据库进行交互了







本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

Web 中文字体应用指南 在 Web 上应用字体是一项基本技术,同时也是一门艺术。对于英文字体来说可选择的范围实在是太广泛了,合理的使用它们将会为你的网站增色不少。关于英文字体的使用和搭配技巧,在这

关键字: php学习记录 yii

public function actionPath() { echo YII::getPathOfAlias('system'

关键字: php学习记录 yii

class GoodsController extends Controller { public function filters() {

关键字: php学习记录 yii

在一个完整的网站中 会有很多重复的部分 如:网站的首尾,一般使用layout实现  还有一些  比如说 下拉列表,还有一些菜单 这些可以卸载不同的 view 文件中 在那些 页面中使用 只要

关键字: php学习记录 yii

Web开发的过程中, 经常会用到验证码, 以防止机器人不断的提交数据, 造成网站的瘫痪. Yii里提供了一个验证码的插件, 就是Captcha. 在项目中使用Captcha需要以下一些设置: 在C

关键字: php学习记录 yii

1.给页面添加.html后缀(伪静态),有利于搜索引擎优化源地址: http://localhost/cms/index.php?r=goods/category美化后 http://localhos

关键字: php yii 框架

1.在布局中(你要显示的地方)2在view中

关键字: php yii 框架

我们可以缓存整个页面,但是只有一个小的区域,会根据不同的条件显示不同的信息,这个小的区域我们需要设置动态缓存。renderDynamic('isGuest');?>源码: public func

关键字: php yii 框架

说是很重要的,经常使用数据缓存,而不是其他的片段和页面缓存,额好像我不太喜欢这种缓存额            function getGoodsInfo($id)             {     

关键字: php yii 框架
关闭
关闭