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齠黈鼿齈鷣鷟龞龝龙龑黥魅鹪龙鹪鴑

本书共88小节:

评论 (写第一个评论)