yii的db解读

来源:互联网 发布:知乎怎么注册 编辑:程序博客网 时间:2024/05/21 09:53
1.yii中与数据库连接的时候,是通过ActiveRecord进行连接的,一般需要与数据库表进行对应的类需要继承ActiveRecord,而对于该表中数据库的查询,同样也是在该
User类中定义的查询方法,不同的是,该查询的方法定义是static的。

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


<?phpdefined('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文件内容是这样的:

<?phpreturn [    '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);
至此就创建了该对象!!!可以与数据库进行交互了





0 0
原创粉丝点击