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
- yii的db解读
- Yii 2.0 视图的深入解读
- YII多DB
- yii2的yii\db\ActiveRecord::deleteAll()请慎用
- Oracle DB Time 解读
- 【Yii】AR模型和db
- Azure Cosmos DB技术性解读
- yii框架:CDbConnection failed to open the DB connection: could not find driver的解决办法
- 二、YII框架多DB设计
- YII2-数据库yii\db\Query 查询方法
- 全面解读Oracle同义词的概念作用、创建删除查看及Oracle的db link
- 全面解读Oracle同义词的概念作用、创建删除查看及Oracle的db link
- Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] No such file or directory’错误的解决方法
- YII框架分析笔记4:ar模型和db
- YII通过db执行sql语句可以看下面
- 报错 Exception 'yii\db\Exception' with message 'SQLSTATE[HY000]
- YII2.0框架(四) 数据库操作yii\db\Commond
- yii基本添加,修改,删除,查询db语句
- 剑指Offer----面试题35:扩展----删除字符串中所有重复出现的字符 & 变位词
- Excel中单元格添加下拉列表的方法
- web 前端入坑第一篇:web前端到底是什么?有前途吗
- 网络编程释疑之:单台服务器上的并发TCP连接数可以有多少
- MTK平台Camera app中的模式切换概述
- yii的db解读
- java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/tv.danmaku.ijk.media.sample-2/lib/x86/libij
- OC 异常消息转发处理流程
- HDU-5778 abs
- js把毫秒转换为 xx小时xx分钟xx秒的通用方法
- 搜索(4):A*
- bulk collect
- binder学习笔记(十)—— 穿越到驱动层
- java.lang.NoSuchMethodException