yii的db解读

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

2. 对于yii与数据库的链接是在配置文件中已经描写过的


<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../../common/config/main.php'),
    require(__DIR__ . '/../../common/config/main-local.php'),   //我的数据库配置是在这其中的
    require(__DIR__ . '/../config/main.php'),
    require(__DIR__ . '/../config/main-local.php')
);
(new yii\web\Application($config))->run();

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

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=10.1.20.36;dbname=db_XXX',
            'username' => 'weihu_dev',
            'password' => 'xxxxxx',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            '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' => 'yii\db\Connection',
    '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);
至此就创建了该对象!!!可以与数据库进行交互了





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世纪殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值