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中的

public static function configure($object, $properties)
{
foreach ($properties as $name => $value) {

$object->$name = $value;
}
return $object;
}
public function setComponents($components)
{
foreach ($components as $id => $component) {
$this->set($id, $component);
}
}
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));
}
}
'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',
]
public function getDb()
{
return $this->get('db');
}
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;
}
}