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齪黔齃齏鷸鷶龤龠龞龘黳魗鹯龞鹯鴓

本书共71小节:

评论 (写第一个评论)