1.yii中與數(shù)據(jù)庫(kù)連接的時(shí)候,是通過(guò)ActiveRecord進(jìn)行連接的,一般需要與數(shù)據(jù)庫(kù)表進(jìn)行對(duì)應(yīng)的類需要繼承ActiveRecord,而對(duì)于該表中數(shù)據(jù)庫(kù)的查詢,同樣也是在該
User類中定義的查詢方法,不同的是,該查詢的方法定義是static的。
2. 對(duì)于yii與數(shù)據(jù)庫(kù)的鏈接是在配置文件中已經(jīng)描寫過(guò)的
run();
3.而對(duì)應(yīng)的common/config/main-local.php文件內(nèi)容是這樣的:
?[ ????????'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.整個(gè)初始化Yii::$app->db的初始化過(guò)程是這樣的,整個(gè)配置字符是在main-local.php當(dāng)中的,而其內(nèi)容是在$config中的,這個(gè)過(guò)程是在base/Application中的
Component::__construct($config);最終實(shí)現(xiàn)的是Yii::configure其內(nèi)容是:
public?static?function?configure($object,?$properties) {?? ????foreach?($properties?as?$name?=>?$value)?{ ????????$object->$name?=?$value; ????} ????return?$object; }
$object的實(shí)參是$app而其中有一個(gè)參數(shù)是component,而對(duì)component的參數(shù)的設(shè)置,還有一個(gè)真正的函數(shù)覆蓋,setComponent,該函數(shù)的定義是在ServiceLocator,因?yàn)锳pplication的父類是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)); ????} }
這樣我們就知道會(huì)有一個(gè)$id是"db"??而$definition則是,具體db的數(shù)組
'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?當(dāng)中,當(dāng)我們不使用的時(shí)候,存放的就是數(shù)組,當(dāng)我們使用的時(shí)候,則會(huì)調(diào)用ServiceLocator的get函數(shù)。
當(dāng)我們調(diào)用Yii::$app->db的時(shí)候出發(fā)?base/application中的
public?function?getDb() { ????return?$this->get('db'); }
函數(shù),而get函數(shù)則會(huì)調(diào)用父類的get函數(shù)
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; ????} }
之前將數(shù)據(jù)庫(kù)的配置放在了$_definition當(dāng)中,那么此時(shí)會(huì)判斷如果??$definition?是數(shù)組,那么就會(huì)創(chuàng)建對(duì)應(yīng)的對(duì)象Yii::createObject($definition);
至此就創(chuàng)建了該對(duì)象?。?!可以與數(shù)據(jù)庫(kù)進(jìn)行交互了