11. 数据库系统及其使用

Symfony龦龤龣龠龜龚龖龕drupal8龃齿齾齽齺齷齴齳齯齭齩php齤pdo齢齟齤龣龠龜齝齚龕齙齕齒齴齳齯龣龠龜齑齎齌龕齉齆齄齀鼿鼻齚齳齤鼺鼷鼳鼰鼮龣龠龜龕鼬鼨鼻鼤鼡齌鼿鼻齤鼟鼞鼛龣龠龜龕鼚鼙鼗鼿鼻鼓齙齕齒齤鼏鼎鼊鼇齎鼺鼅鼁鼀龣黼黻黺龕黶鼙鼗黳黱黯黮黫黩齤龣龠龜黦龕黥黤黡 MySQL 黱黗Oracle鼁MS SQL Server龕黇鼙黅黁麾鼻齌麽麺麶麳麰麯麫麪麧麦齕齒齴mysql麜pgsql麜sqlite麑麍龕
齙麌齕齒麉麅龣龠龜麄麁鹽麑麍龕鹼鹹齤鹶鹴鹰鹭鹩鹨鹦齺齷龕麳鹥鹡齙鹝鹛鹗鹕齆鼇齀鹔鹐鹨龤鹌齴鹉鹆

1鹆php齤PDO齢齟龕鸷鼺鸴鸲鼟鹆 http://php.net/manual/zh/intro.pdo.php
鸤鼟php齌鸣齕齒齤龣龠龜齑齎齌龕鼻齀齚齳鸢黫龣龠龜齤鼰鼮龕drupal8齤龣龠龜齝齚鼟齭齩齙齺齷齤龕齴鹉齙鸠麳鹥鹡齤鸝鸛麪黻齄齀鸘鸗鸔鸓鼞鼛鸑鸏鼟鸌鸊齤齀鸆齙鸅鼟鸃鼛鸁鷽齤

2鹆SQL鷷鷳
SQL99麌鷭鼮SQL:1999麜SQL3 麜 SQL-99. 鼟sql鷦鷤齤鷷鷳 https://en.wikipedia.org/wiki/SQL:1999龕 鷞龤ANSI SQL:2003龕鷓鸅鷒鷏SQL鷦鷤齤鷷鷳鷎龣龠龜麾鷌鷊龤齤黺鷈龕齴鹉鸤齀鸠麳鷄drupal8龣龠龜齤sql鷦鷤鷃鹗黦黻鼨鷂鷀鶽齴

3鹆鶺龖鶷鶵鶴鼷
麳drupal8齤鶷鶵麪鶲鶮鼟鶴鼷龕龣龠龜齝齚麪鶬鶫鶧鶴鼷鶤鷈鶣鶢龕齴鹉鸤鸠鷄麽麺鷂鶡鶟鶞鶝龕鷂龤鹩鶞鶚鶡鶙齤鶘鶔龕鶐鼨鹗鹕鶌鶋鶈鶄齤鶁鷄龕鵿鵾鹩麉鵻鵸鵴齀龕鵰齯鵯鵸黻鵮齉齆鵪鵩龕鵥鵤黦鵣

drupal鸷鼺齤龣龠龜鵡鵝鹆 https://www.drupal.org/developing/api/database 龕鵗鵖D7鷎D8麫鵓鶋鵐鵎鵍龕鷄龃鵓鵡龤鼨鸘鸗齤鵊鶲鵈鵄鵀鴽麶

鹔鸣鴺鷄齳鷄龣龠龜鴸鼏鴷鴵齤鷒鷏鹆

鷒鷏鴲齩鴮鴬鸁鴨鵡龖麪/sites/default/settings.php龕齆齄齀鷄鶲黫鴤鹐鹨鴣鴢鴠齤鷒鷏鹆

$databases['default']['default']齤鴕鼟齳齯龣龚龕鸤齯龣龚鴑鹝鷈DSN鼊龣龠鴐鴌鴑Data Source Name黼龕齙麽鴊齳齯龣龠龜龕鴉鴇鴸鼏鹌鼙齤鹌龤鴷鴵

鷈鴆鴄鹼鹹鸠鸣鴁鸅鳿鸤齯龣龚鴊鳻黗$dsn

鳸鵾鸣齤麽麺鹆$databases['default']['default']=$dsn鳲齄鵮鷂齉齆鳯鶽龕鷈鼞鼛鼟鳭鳬龣龚鳨龤鼞鼛鴇鷏鳨

鸤黻鼟drupal齕齒麉龣龠龜麑麍齤鳧齷龕齀鹔鴁鸅鼻 $databases[$key][$target]=$dsn鳲 鳦鴊鳻

$key鳢齳齯龣龠龜龕麽鴊齳齯鹹鳞齝齚龕鼨鵐齤$key鳝鳚鼟鼨鵐鳙鷃齤龣龠龜龕黶鷎drupal齀黮齤鳗鳓鼺齝齚鳐鼮黦鳏鷓齝齚齤龣龠龜黻麾鷌鼟鼨鵐齤$key

麧麦齤drupal鼚龤齳齯$key龕齙鳍鳊鴌鷈default龕齙鼟drupal鼿鼻齤龣龠龜

$target鼟鼞鼛鳇鳨齙鳅鼗鼻齩鹶鹴鹰鹭龕齳齯$key鼇麾齤龣龠龜龕鷓鸅龤鳂鵐齤鳙鷃鷎龣龠龕齄齀鳍鳀麯麳麉麅麄麁鹽鵾鸣龕齀鲽鶡鲻鲹

鵈鼛鲶齳麅麄麁鹽黻鼇麾齳齯$target龕黥黤MySQL龣龠龜鹐鹗齴齳齯鳅鲵鲴鲳齤鲲鵮龕麳齳齯麄麁鹽鵾鸣齤MySQL齺鲯齄齀鲫鲩鵐鲥鶲鳏鷓麄麁鹽

鵈鼛黻齄齀龤麉齯麄麁鹽鲢龤鳂鵐齤龣龠龜鳙鷃鷎龣龠龕麳鲠鲝鲜鲙鲗齤黦鲖鵮鳀鲕鲓鲑齺齷鹶鹴鹰鹭龕鶐鲝鲜黳鲍齳黫齤鲙鲗鼚鵮麳鳅麄麁鹽鵾鸣鲌黺

鼊鲋齩Mysql龣龠龜齤鳅鲵鲇鲃鲁鱾鶋鱺鷄鱸鱶鱴鵮MySQL鱰齳鶁Baron Schwartz龕Peter Zaitsev龕Vadim Tkachenko 鱤鳲麪鵡龤鱢黼

鵪鵩$databases['default']['default']齤鵖鱟齴鱛龕鸤麌鼟鶋麉龣鵎黩鵿鴮鹌鼙鼗齤鸁鴨

鴊鳻龤齳齯鷭鸊default齤龣龠龜龕鸤齯龣龠龜鼚龤齳齯鷭鸊default鱗鷷齺鲯齤麄麁鹽麳鲌黺龕鹌龤鱓鲝鼰鼮鱒麳鸤齯麄麁鹽鵾鸣麶

$databases['default']['default'] = array (  
  'database' => 'drupal',  
  'username' => 'yunke',  
  'password' => 'yunke',  
  'prefix' => 'yunke_',  
  'host' => 'localhost',  
  'port' => '3306',  
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',  
  'driver' => 'mysql',  
);  

鵈鼛鸃鼛鸁鴨麉龣龠龜鳇鳨齄齀鸤鱐鹆

$databases['default']['default']=$dsn_1; //主服务器  
$databases['default']['replica']=$dsn_2; //用于只读的从服务器  

鵾鸣齺齷齴鱏麅麄麁鹽龕鵈鼛鼗齺齷麉齯鲵麄麁鹽鷌鸃鼛鼰鼮鳨齄齀鸤鱐鹆

$databases['default']['default']=$dsn_1; //主服务器  
$databases['default']['replica']=$dsn_2; //用于只读的从服务器  
$databases['default']['yunke']=$dsn_3; //用于只读的从服务器  

鸤鱐鱎鱊齄齀龕鶐鼟鲶鱈鱅鱁鰾鼏黦鱒鼗鳢鷒$target鹩鼨鼺鰻龕

鰺鰸鵐齳齯$key鹔鸣鼚龤鳅麄麁鹽齄齀鲝龕麉齯$target鲵麄麁鹽鼟齳鱐齤龕鰸鱒鼟鼚鱓龕鹌齀齭鵓鼿鼻鹐鹨鴣鴢鴠麪齤鼺鼷鹆

$databases['default']['default']=$dsn_1; //主服务器  
$databases['default']['replica'][]=$dsn_2; //用于只读的从服务器  
$databases['default']['replica'][]=$dsn_3; //用于只读的从服务器  
$databases['default']['replica'][]=$dsn_4; //用于只读的从服务器  

drupal麳齳齯$target鹔龤麉齯$dsn黦龕齙鷂鰷鰵鰴鰰齳齯龕黻齺齷齴鹶鹴鳀鲕龕黤黇鰮鰫鼙鼗齭鵓鼿鼻齀鵾鼺鼷

鵗鵖鹆麳齳齯$key鹔鰪鰧鼗龤齳齯$target鳍鳊鴌鷈default龕鰸齙鼮鷈鳅麄麁鹽龕麳鸛鰣鹐鶈黶鳏鷓$target鼨齄鼻黦麧麦鰢鰠鶲default

鲋齩鲝鲙鲗齤鹶鹴鹰鹭黥鰝鲇鰜龕黥黤龣龠鳀鰘鰖鰒龕鼙鼗麾鼻齌鰑鰍龕鷒鷏鰌黮齤鰉龕鵊鷄鵾鸣鲁鱾齤鶁

鵪鵩齴$key鷎$target鸠鴁鸅鷄齳鷄$dsn鸤齯龣龚鼟鸃鼛鷒鷏齤鹆

$dsn= array (  
  'database' => 'drupal',  
  'username' => 'yunke',  
  'password' => 'yunke',  
  'prefix' => 'yunke_',  
  'host' => 'localhost',  
  'port' => '3306',  
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',  
  'driver' => 'mysql',  
); 

齀鵾鼟鰅齭鵓齤鴷鴵龕鶋鶈鳀齆麾鷌鵮鷄鰄龕鳅鼗鰁齀鹔鹐鹨鹆

鴊鹛鯿鼚龤齳齯鯻鯷鯴鴕齤黦鲖龕鴊鳻鹌龤龣龠鴊鹰鼿鼻鷌鹛鯿龕鼨鼻鵊鷈鯳鼁鶧鵤鯱鷌龣龚鯮鯫

鹩鯧齤鼟drupal麑麍鷈鼨鵐鴊鲌鼻鼨鵐鹛鯿龕鼿鼻黤鹔鹆

'prefix' => array("default"=>"麧麦鹛鯿","齭鵓鴊鴌"=>"鰮鷒鹛鯿","齭鵓鴊鴌"=>"鰮鷒鹛鯿"),

麳鸛鰣鹐鶈黻鳍鯗鯓黗鸤鱐齤鯒鼷龕鳏麪default鰪鼨齄鯐龕鼻齩鳢鷒龦龤鰮鷒鹛鯿齤鴊麧麦鼿鼻齤鹛鯿龕鼨鼙鼗鵊='',

鳢鷒鴌鯻鯳鵣鹆'namespace'=> 'Drupal\\Core\\Database\\Driver\\mysql',

鸤齯鴊鳻drupal龣龠龜齑齎齌鼿鼻鰮鷒龣龠龜鯉鯇鸛鰣齤鴌鯻鯳鵣龕鼨鯃麌齄齀龕鶐鰅鼓鯃鵾龕齄齕鱶鸛鰣鲩鯀龕鮽鮺齾鯇鳝鸝鮶鮲鰵鲃鱁鶲

麳$dsn麪鷞齄齀鳢鷒鳏鷓鸁鴨鮮龕鳝鳚鼨鵐龣龠龜龤鼨鵐鸁鴨龕鶐麌龤齳鮬鳝鼻鸁鴨龕鹔鸣鮫鮫鳚鼻齤MySQL齄鼿鼻齤鸁鴨鮮鹆

$dns['_dsn_utf8_fallback'] = TRUE
drupal麧麦鼿鼻utf8mb4龣龠龜鯻鯷鮜麺龕齙鼟utf8齤鮙鮖龕鮒鳢鮐鴊鳻鰢鰠鶲utf8鮜麺龕鼨鼿鼻utf8mb4
麧麦鼿鼻utf8mb4龕黤鮍鮋鮊鼿鼻utf8mb4鸠鼨鵮鰢鰠鶲鮈麫鵓鯻鯷鮜麺齤龣龠龜龕鸤鱐鷂鮄鮁鮙鮖鶈鳀齤龣龠
魽魻鹆 http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
$dns['unix_socket']鼿鼻齳齯unix_socket
$dns['pdo']魸魴齤pdo鰴鮮龕鼻齩魰鲃pdo齤黺鷈
$dns['collation']鲌黺鶷鴨mysql齤鯻鯷鮖鷦鷤黦鹆SET NAMES ' . $charset . ' COLLATE ' . $dns['collation']龕龦龤鸤齯鳢鮐魮鶷鴨鹆'SET NAMES ' . $charset
$dns['init_commands']魪黺mysql齤魨魦鯓鳊鮐
$dns['transactions']鴊鳻鼟魢麑麍鸑麁龕龦龤鶷鴨魮麧麦麑麍鸑麁龕魟鲠鵪魝鶷鴨鷈false齀鼿魙鲃鼨麑麍鸑麁

齀鵾黻鼟鲋齩黤魕鷒鷏龣龠龜鴸鼏鰴鮮齤鹐鹨龕鹔鸣鮫鮫drupal魸魴鼿鼻齤龣龠龜鳊鴌鰑魮鹆

drupal鼿鼻齤龣龠龜鴌麜鴊鴌麜鯻魔鴌鰪鰧鼟鯻魑麜龣鯻麜鹔鰍魍鷎鴬魋
魊鷒鰑魮鼟鹆preg_replace('/[^A-Za-z0-9_.]+/', '', $database);
鷓鸅齤鰫鴌鼨鵮龤鴬魋鼊鰫鴌黻鼟鲙鲗鷦鷤as鸠鸣齤鴌鯻黼龕鰑魮鼟preg_replace('/[^A-Za-z0-9_]+/', '', $field);

怎么进行数据库查询:

麳鬿齯drupal鸛鰣鲌黺鬻魦鬸魔黻鬵龣龠龜齤鸁鴨鴷鴵鵗鲜鶲齴龣龠龜魰鲃黫麪龕黡DrupalKernel龃黗鬯鼻
鷊鳧鼟麳Drupal\Core\Site\Settings 齤 initialize鼺鼅鬯鼻Database::setMultipleConnectionInfo($databases);

黶鼙鼗鲙鲗齤黦鲖鬬鴺鼙鼗鱅鱁龣龠龜鴸鼏黫龕麳鶴鬨麪齆齄齀鸤鱐鼰鼮鹆

\Drupal::database(); //获取配置中$databases['default']['default']表示的链接,这对于大多数只有一个数据库的站点而言是最常用的,全局获取  
\Drupal::service("database"); //完全等同于\Drupal::database();  
$container()->get("database"); //效果同上,在容器对象可用时使用  
\Drupal::service("database.replica"); //获取配置中$databases['default']['replica']表示的备用数据库链接,无设置将回退到主库  
$container()->get("database.replica"); //效果同上,在容器对象可用时使用  

齀鵾鼟鳚鼻齤鲫鬤鼺鼅龕黳鯃鬡鬟齤鼺鼅鼟鹆

\Drupal\Core\Database\Database::getConnection($target, $key); //这样可以指定任意目标数据库  

麳鱁鶲鰾鼏鼇齎鸠黻齄齀鼿鼻齙鸊鲙鲗齴龕麳鸷鼺鵡鵝麪鲙鲗鳀鷈鱏鶋黫鹆鬝鬛鲙鲗麜鯇鬛鲙鲗

鳏齺鸤齯鴌鯻龤鮬鷀鶽鬙龕鹌鬖鬝鬛鲙鲗黻鼟鬕鼏鲝SQL鷦鷤鲙鲗龕鼬鯇鬛鲙鲗鼟鬯鼻齝齚齕齒齤鷦鷤鷃鹗鼺鼅鬑鲥鷃鹗SQL鷦鷤鬏鲙鲗

鴺鷄齳鷄鹌鬖齤鬝鬛鲙鲗龕麌黻鼟鬕鼏鲝SQL鷦鷤齤鲙鲗鸃鼛鸊鹆

麳鵾鸣齤SQL麪鴊鴌鹰鼟鳍鬋麳鬉鬇魋鵄鸣龕鼟鼨鬃鹛鯿齤鴊鴌龕齝齚鬂龠鸤鱐齤鷦鼅齾鯇鬁鯃鴊鹛鯿龕黇{}鼨鷂鬁鯃鹛鯿
鬉鬇魋鵄鸣鼨鵮龤鯳鯒龕鱏髸鼙鼗髴髰鯐齳齯鯳鯒龕麳SQL鷦鷤麪鳏鷓鶈鳀鼨鵮鼿鼻{}龕髭髭鳍鴊鴌鼿鼻
麌鷄鶲齄齀鼿鼻髩龣髧髤龕髩龣髡鴲鯷鴌齀髠魋鬻魦鼨鯃髟魋龕麳鲋髝龣龚麪鯮鯫髛鰣龦龤鲋齝龕鸤鱐齤鶷鶵齝齚齾鯇髚髖鵗鲜髓髏龕黇鼙鯗鷏髩龣鴕
drupal8齄齀齉鴁鸅鼿鼻龣龚鼺鼷齤髡鴲鯷龕鷄鵾鸣鰅鸠齳髋鷦鷤龕鸤麳PDO麪鼟鼨魸魴齤龕drupal鷂鷈鴁鸅齾鯇齟鬻龣龚

鬝鬛鲙鲗鱎鱊鬕髈齴黶龕鶐龤鱏齯髇鴬鹆

鳗齳鹆齙黇鼅齉鶴鬨鲙鲗髄鵸鼇SQL鷦鷤髃黺黅黁

鳗骿鹆鬕鼏鲝齤SQL鷦鷤齄鵮鼨鷂骽鹨鹌龤齤龣龠龜龕鵈鼛麳黳黱龣龠龜黫黩齤黦鲖鬃鳦骹骸

$con=\Drupal::database();  
$con->query("SELECT nid, title FROM {node} WHERE type = :type", array(':type' => 'page'));  
$con->query("SELECT * FROM {node} WHERE nid IN (:nids[])", array(':nids[]' => array(13, 42, 144))); 

鹌齀齭齩鵾鸣齤骴骳龕鲁鱾鼿鼻鯇鬛鲙鲗龕drupal鷈鲶齳骯黫黩齤鲙鲗鱒鷳鲴齴齳齯黫龕齙龤魴麉鼺鼅鳦骬鴢鷃鹗鲙鲗鷦鷤龕黥黤鹆

$con=\Drupal::database();  
$query = $con->select('users', 'u')  
  ->condition('u.uid', 0, '<>')  
  ->fields('u', array('uid', 'name', 'status', 'created', 'access'))  
  ->range(0, 50);  
$result = $query->execute();  
foreach ($result as $record) {  
  // Do something with each $record  
}  

鯇鬛鲙鲗黫鴤齩CI骨骤齤鬟鯇骠骝黫龕齙鼨鼙鼗鰄鱁SQL鸃鼛鲝龕骙龠齕齒齤鼺鼅鷃鹗骕齄

麳鸤齯鲯鵸麪$con->select(黼鼺鼅骑鰢齳齯select鲙鲗鷃鹗鼇齎龕鸤齯鼇齎骎鼻鰾鼷鬯鼻齾齽齤鼺鼅骬鴢鰅骋骇骄齳齯鲙鲗sql

黤鮍鼺鼅骑鰢齤鼟鼇齎鵓骂魮齄鼿鼻鰾鼷鬯鼻龕龣龠龜鴸鼏鼇齎齄齀骑鰢麉骯鲙鲗鷃鹗鼇齎龕鷓鸅龤鼨鵐齤骬鴢鼺鼅龕黤鹆

鸤鮬鲙鲗鷃鹗鼇齎齤鷒鷏麳鸤鵄鹆\core\lib\Drupal\Core\Database\Driver\龕
齙鸅骀驼齾魑黫\core\lib\Drupal\Core\Database\Query龕魑黫齕齒鸢龣龠龜鳂鵐鲲鵮龕鵸黫鹉驻鼨鵐龣龠龜齤鰮驹鱴
鸤鮬鲙鲗鷃鹗黫鸢齾鷒鷏齴鹩麉鼺鼅鼳鷃鹗齾齽黫黩齤sql鲙鲗龕鸤鮬sql鷦鷤鼟驷鵴龣龠龜黫黩齤,
鵾鸣齤鸤鮬鲙鲗鷃鹗鼇齎鱒齄齀鳝鸝魙鲃黫黩鯗黱(string)$var鳦鱁鶲鷃鹗齤SQL鷦鷤龕麳鷓鸅鹐鶈鼻php驶鰒鼺鼅__toString()鳦齺齷鮒鱗齤龕齺驵鵾鰅骋黻鼟鼿鼻鷌鼺鼅鱁鶲SQL鷦鷤驳髧髤驯Connection鴸鼏鼇齎齤麪驭鲙鲗鼺鼅鼳魪黺麶

鼗鬵鲶齯鷃鹗鼇齎鸆鷓鸅齤鷃鹗鼺鼅驪驩龃龕鼙鼗鹩鶋驧驦龕鸤鵄鼨鸊驪驩龕齆齄齀鶲鸷鵿鵡鵝鲙鷄龕黤鼙驣鲜驢鹉鬕鼏鷄黫鷒鷏麽麺鱛龕齙龤魽驠齤鵗驝龕鸷鵿齤API鵡鵝黻齕驚齾鸤鮬鵗驝麶

鯇鬛鲙鲗魟齴骬鴢鷃鹗骽鹨齤SQL黮鷞齄齀鬁鯃鲙鲗鷷驙龕鸤齯鲲鵮齄齀齉drupal鶴鬨龠鮒黅黁鳂麾齤鲙鲗SQL

$con->select($table, $alias = NULL, array $options = array()); //构建查询对象  
$con->insert($table, array $options = array()); //构建插入SQL语句对象  
$con->merge($table, array $options = array()); //构建合并查询  
$con->upsert($table, array $options = array()); //构建upset查询对象,数据库不支持则模拟  
$con->update($table, array $options = array()); //构建更新查询对象  
$con->delete($table, array $options = array());  //构建删除查询对象  
$con->truncate($table, array $options = array());  //构建清空数据表查询对象  

创建数据库及表结构:

鼗驘鹗齳齯龣龠龜龕齀鸆鷒鷏鵄鸣齤鴊鳙鷃龕麳drupal麪驗驗鼨鼟鳝鸝鬝鬛鲙鲗鲲鵮齺齷齤龕drupal龣龠龜鰾鼏鼇齎齕齒齴齳齯鼺鼅鳦骑鰢schema鼇齎鹆

schema鼇齎鼰鼮龣龠龜鳙鷃鷒鷏龕鸤鼟齳鬨鹩驕鼗齤鹐鹨龕黡齩驧驦龤驔龕鳿麳鹔齳驧鴐麺鳀驑麪驏驌驪驩龕齭鵓鼿鼻黤鹆

$con=\Drupal::database();  
$con->schema();

开启数据库事务:

龣龠龜鸑麁齉鲙鲗鷊鲴骴鵸鱴麜齳驋鱴龕麳drupal麪麧麦鼟麑麍鸑麁齤龕魟鲠麳鰾鼏鰴鮮麪鵪魝驊髖鸑麁龕mysql麧麦鼿鼻麑麍鸑麁鶮驢齤InnoDB驆驃髟驂

鼨鼗鼿鼻$con->commit();鼳齕驁齳齯鸑麁龕鸤鷂驀鶢鵍鳚龕齝齚鷂騿鼷齾鯇齕驁龕
鼙鼗鵗鵖齤鼟DDL鷦鷤鼊龣龠龜鷒鷏鷦鷤黼麳鶋麉龣龣龠龜麪鼟鼨麑麍鸑麁齤龕鴉鬇MYSQL

$con=\Drupal::database();  
$yunke=$con->startTransaction($name = ''); //开始事务,参数指回滚点,只要变量$yunke不被销毁那么事务持续开启,一旦销毁即被提交  
$con->rollback($savepoint_name = 'drupal_transaction')  //回滚事务到某个回滚点  
$yunke=NULL;//变量被销毁,事务被提交  

使用结果集:

SELECT鲙鲗骑鰢齳齯鼁麉齯龣龠龕鸤鮬鳙鮍鮖鳍鴉鶬麳Drupal\Core\Database\Statement麪龕齙骀驼齾PDO齤PDOStatement黫龕齄齀鼿鼻齙齤齿鶈鼺鼅龕黺鷈騹鶲鲙鲗鰴鮮齤魰鲃龕鳝鳚鼿鼻foreach騶騴鼳鱅驚鳙鮍龕黤鹆

黳麉鳙鮍鮖齤鼰鼮鵊鲙鷄鹆 https://www.drupal.org/docs/7/api/database-api/result-sets

$con=\Drupal::database();  
$result = $con->query("SELECT nid, title FROM {node}");  
foreach ($result as $record) {  
  // Do something with each $record  
  $node = node_load($record->nid);  
}  
$record = $result->fetch();            // 使用默认fetch模式获取下一行数据.  
$record = $result->fetchObject();  // 以stdClass对象形式取回  
$record = $result->fetchAssoc(); //以数组方式取回  
$record = $result->fetchField($column_index); //获取一行中的一个字段,$column_index是列索引值,以0开始  
$number_of_rows = $result->rowCount(); //计算 DELETE、INSERT 、UPDATE 影响的行数,SELECT不应该使用这个方法  
$con->select('users')->countQuery()->execute()->fetchField(); //SELECT应该使用这个方法,在countQuery()调用前需要构建好查询  

数据库操作的过程式包装:

php齤鼀龣鼟齿騱齄鼻齤龕鷈齴鼺鰻鼰鼮龕drupal鬵魴麉龣龠龜鼰鼮鲲鵮騭鶬鶲鼀龣麪龕鸤鱐黻齄齀麳騫魕鸴鼺鬯鼻齴

鸤鮬鼀龣鷒鷏麳/core/includes/database.inc鷎/core/includes/schema.inc麪龕齙鸅麳HTTP騦驭騢騡齤騠鶮驢齌鳍鹴鲜

齆齄齀麳鶴鬨麪鬕鼏鼿鼻龕魽鸏魻鸤鱏齯鵡龖

鵾鸣齭鵓鰁鶲鼿鼻齌鸣齤鹐鹨龕鹔鸣鴁鸅鷄鷄鴐麺麯騱鹆

麳drupal麪龣龠龜鴐麺鴲齩鹆\core\lib\Drupal\Core\Database龕
鳏麪Driver鵸鱗骝驃鬋鼨鵐龣龠龜齤鯉鯇龕鹉驻騜鵍鲑騘鼊龣龠龜鼺騖黼龕Query鵸鱗骝驃鬋SQL鲙鲗鷦鷤鷃鹗黫龕驏驌鷃鹗SQL鷦鷤龕鹌龤齤魪黺騒騏鶲鰾鼏黫Connection齤query鼺鼅鵾龕鼬鮒鼺鼅鷞鼨鼟騎騌魪黺鲙鲗齤鼺鼅龕鲙鷄鴐麺鹆

$stmt = $this->prepareQuery($query);  
$stmt->execute($args, $options);  

鸤齯$stmt鳏齺鼟Drupal\Core\Database\Statement鼇齎龕鷈鼞鼛鼟鸤齯鳨麳鰾鼏鼇齎鷃騈鼀龣麪龤鹆

鸤齯$this->statementClass黻鳢鷒齴Drupal\Core\Database\Statement龕鸤齯鶷鴨黻鼟齉pdo骑鰢鸤齯鼇齎
黤鮍鷞鼨鵪鵩龕鵊鷄鹆 http://php.net/manual/zh/pdo.setattribute.php

$connection->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array($this->statementClass, array($this)));  

鹌齀鰅骋魪黺drupal鹌龤鲙鲗齤鼟Drupal\Core\Database\Statement齤execute鼺鼅

鲙鲗騄騁骠骝黻鶷鴨麳鸤齯鼺鼅鵄鸣龕黤鮍鼙鼗鬻駾鲙鲗騄騁骠骝齄齀鸤鱐鹆

\Drupal\Core\Database\Database::startLog($logging_key, $key = 'default');   
//开始日志记录 配置中的一个$key对应一个日志记录器,$logging_key可以是$target也可以自己随意指定  
\Drupal\Core\Database\Database::getLog($logging_key, $key = 'default');  //得到查询日志

鲙鲗騄騁鼟齳齯龣龚龕鹐鹨黤鹆

麳鬯駺齤黦鲖鼿鼻齙鲠鳚鼺鰻龕齄齀鼻drupal齤騄騁齝齚鼳驆驃鸤齯騄騁

array(  
        'query' => "查询语句",  
        'args' => "参数",  
        'target' => $target,  
        'caller' => "调用者",  
        'time' => "查询这条语句执行的时间",  
      );  

黤魕鳿drupal齤龣龠龜齝齚齕驚鶢鳦鳨

黤鮍齆鶔鱁drupal齤龣龠龜齝齚鹩鼓龕駸齕驚鶢鳦鼻麳齾齽鳏鷓齤鮮鱗鵾鸣龕齆鳿鼙鼗鶮驢鹔鸣齤駵鼮鹆

1鹆drupal龣龠龜麽麺麳龣龠龜麪鹗駱齴齳齯sequences鴊龕鼻齩驷鵴nextId鼊黼齤鲲鵮龕齕齒齳齯黥鹝鹛骑鰢齤龣鶋齤駰齳鬿龣
2鹆麳durpal麪鶴鬨齄齀黅黁鲙鲗龕龣龠龜麽麺鼇鲙鲗齕齒鷷驙鼤驢驳駬麰鶴鬨黅黁龕麳preExecute鼺鼅麪鬯鼻
\Drupal::moduleHandler()->alter($hooks, $query);黅黁鲙鲗鷦鷤龕鶴鬨鳝鸝鸤鮬鷷驙黅黁鲙鲗龕

鶮驢鼓鸤鮬鷎drupal齝齚駪駩駧鷽齤鸴鼺黻齄齀齕驚鶢鳦鹹鳞鼿鼻齴

鵀鴽鸔駥鴬鹆

1鹆齄齀鼿鼻db_ignore_replica()鼀龣驊鼻鲵龜龕鮒鼀龣鴲齩core\includes\database.inc龕麳鸁鴨鵡龖麪齄齀鶷鴨maximum_replication_lag鳦鳢鷒鲵龜齤駡駝黦鵣龕鼨鶷鴨麧麦鷈300駜龕鸤齯鼀龣鷂鹗駱$_SESSION['ignore_replica_server']龕麳鸠駛駘鸣麪骙龠鮒魊駕鼟魢驊鼻鲵龜龕齙齤鴕鼟鶷鴨黦齤鵊駒黦鵣鯃駡駝黦鵣龕麳鸤齯黦鵣鹐齝齚鼨鷂鼿鼻鲵龜龕鮒魊駕鼟麳騦驭駑麰kernel.request鸑龖黦髃黺
2鹆齷麳鮋鮊鼨鬏鼿鼻鰒鷦"master/slave"鼬鼿鼻"primary/replica"龕黡齩鵡鯓骴骳龕鵊鷄鹆 https://www.drupal.org/node/2275877

齀鵾黻鼟drupal8龣龠龜麽麺齤鹌龤鸔駥龕鼨駎鹝鶮鳂鴷鮋鮊齄齀鶡鶙鳝鸝鴐麽麺鼁鸷鼺鵡鵝鲙鸘鸗齴
鹔齳驧鳿驪驩龣龠龜齤Schema龕齙鼟齳齯API龕驪驩黤魕麳drupal齤麾鼻齌鷒鷏龣龠龜龕黤魕鹗龜鹗鴊

本书共78小节:

评论 (写第一个评论)