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