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齡黀鼴齀鷣鷡龦龢龟龘黜魙鹪龟鹪鴏

本书共63小节:

评论 (写第一个评论)