- accordion(1)
- active-trail(1)
- ajax(1)
- angular(1)
- apache(3)
- bash(4)
- block(2)
- browsers(1)
- button styles(1)
- checkboxes(1)
- code key(1)
- compress(1)
- console(4)
- css(1)
- ctools(1)
- date(1)
- db_query(2)
- db_select(2)
- debian(1)
- dom function(1)
- drag and drop(1)
- drupal 6(3)
- drupal 7(3)
- drupal functions(6)
- drupal_goto()(1)
- drupal_mail(1)
- errors(1)
- eslint(1)
- fancybox(1)
- fedora(1)
- firefox(2)
- firefox addons(1)
- firefox profiles(1)
- form(1)
- form api(5)
- format_date(1)
- git(1)
- hooks(2)
- hook_mail(1)
- hook_theme(1)
- hotkey(1)
- httpd(1)
- husky(1)
- ie(2)
- javascript(2)
- jquery(7)
- jquery plugin(2)
- jquery regex(1)
- js templates(1)
- kb(1)
- like button(1)
- link(1)
- linux(4)
- linux commands(2)
- list(1)
- list styles(1)
- markup(1)
- menu(3)
- mobile(1)
- modal(2)
- module develop(2)
- modules(1)
- monorepo(1)
- mootools(1)
- mysql(2)
- node_save(1)
- opacity(1)
- opensuse(5)
- opera mini(1)
- padStart(1)
- page.tpl.php(1)
- password hash(1)
- patch(1)
- pdf(1)
- python(1)
- radiobuttons(1)
- reg_ex(2)
- sass(1)
- scripts(1)
- search form(1)
- selenium(1)
- share button(1)
- sms(1)
- ssh(2)
- styles(4)
- switch case(1)
- table style(1)
- tar(1)
- taxonomy(1)
- taxonomy menu(1)
- templates(1)
- theme(4)
- toggle(2)
- touchpad(1)
- tray(1)
- trigger(1)
- typescript(2)
- typescript-eslint(1)
- ubuntu(3)
- usefull function(1)
- views(1)
- virtual hosts(2)
- virtualbox(2)
- vmware(1)
- webdriver(1)
- youtube(1)
- кроссбраузерность(1)
Да простит меня музицирующий друпальщик
Выборка с условием:
// Drupal 6
$nodes = db_query("SELECT nid, title FROM {node} WHERE type = '%s' AND uid = %d", 'page', 1);
// Drupal 7, вариант 1
$nodes = db_query("SELECT nid, title FROM {node} WHERE type = :type AND uid = :uid", array(':type' => 'page', ':uid' => 1));
// Drupal 7, вариант 2 (более правильный)
$nodes = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('n.type', 'page')
->condition('n.uid', 1)
->execute();
$nodes = db_query("SELECT nid, title FROM {node} WHERE type = '%s' AND uid = %d", 'page', 1);
// Drupal 7, вариант 1
$nodes = db_query("SELECT nid, title FROM {node} WHERE type = :type AND uid = :uid", array(':type' => 'page', ':uid' => 1));
// Drupal 7, вариант 2 (более правильный)
$nodes = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('n.type', 'page')
->condition('n.uid', 1)
->execute();
Выборка из двух таблиц соединённых с помощью INNER JOIN:
// Drupal 6, Drupal 7
$result = db_query("SELECT n.title, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid");
// Drupal 7, вариант 2 (более правильный)
$query = db_select('node', 'n');
$query->innerJoin('users', 'u', 'n.uid = u.uid');
$query->fields('n', array('title'));
$query->fields('u', array('name'));
$result = $query->execute();
$result = db_query("SELECT n.title, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid");
// Drupal 7, вариант 2 (более правильный)
$query = db_select('node', 'n');
$query->innerJoin('users', 'u', 'n.uid = u.uid');
$query->fields('n', array('title'));
$query->fields('u', array('name'));
$result = $query->execute();
Получить значение поля у единственной записи:
// Drupal 6
$title = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", 123));
// Drupal 7, вариант 1
$title = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => 123))->fetchField();
// Drupal 7, вариант 2 (более правильный)
$title = db_select('node', 'n')
->fields('n', array('title'))
->condition('n.nid', 123)
->execute()
->fetchField();
$title = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", 123));
// Drupal 7, вариант 1
$title = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => 123))->fetchField();
// Drupal 7, вариант 2 (более правильный)
$title = db_select('node', 'n')
->fields('n', array('title'))
->condition('n.nid', 123)
->execute()
->fetchField();
Получить объект по его id:
// Drupal 6
$node = db_fetch_object(db_query("SELECT * FROM {node} WHERE nid = %d", 123));
// Drupal 7, вариант 1
$node = db_query("SELECT * FROM {node} WHERE nid = :nid", array(':nid' => 123))->fetchObject();
// Drupal 7, вариант 2 (более правильный)
$node = db_select('node', 'n')
->fields('n')
->condition('n.nid', 123)
->execute()
->fetchObject();
$node = db_fetch_object(db_query("SELECT * FROM {node} WHERE nid = %d", 123));
// Drupal 7, вариант 1
$node = db_query("SELECT * FROM {node} WHERE nid = :nid", array(':nid' => 123))->fetchObject();
// Drupal 7, вариант 2 (более правильный)
$node = db_select('node', 'n')
->fields('n')
->condition('n.nid', 123)
->execute()
->fetchObject();
Посчитать число записей:
// Drupal 6
$count = db_result(db_query("SELECT COUNT(*) FROM {node}"));
// Drupal 7, вариант 1
$count = db_query("SELECT COUNT(*) FROM {node}")->fetchField();
// Drupal 7, вариант 2 (более правильный)
$count = db_select('node')
->countQuery()
->execute()
->fetchField();
// Drupal 7, вариант 3
$query = db_select('node');
$query->addExpression('COUNT(*)');
$count = $query->execute()->fetchField();
$count = db_result(db_query("SELECT COUNT(*) FROM {node}"));
// Drupal 7, вариант 1
$count = db_query("SELECT COUNT(*) FROM {node}")->fetchField();
// Drupal 7, вариант 2 (более правильный)
$count = db_select('node')
->countQuery()
->execute()
->fetchField();
// Drupal 7, вариант 3
$query = db_select('node');
$query->addExpression('COUNT(*)');
$count = $query->execute()->fetchField();
Выбрать определённое количество записей:
// Drupal 6, Drupal 7
$result = db_query("SELECT * FROM {node} LIMIT 0, 10");
// Drupal 7, вариант 2 (более правильный)
$result = db_select('node')->range(0, 10)->execute();
$result = db_query("SELECT * FROM {node} LIMIT 0, 10");
// Drupal 7, вариант 2 (более правильный)
$result = db_select('node')->range(0, 10)->execute();
Обойти записи:
// Drupal 6
$nodes = db_query("SELECT * FROM {node}");
while ($node = db_fetch_object($nodes)) {
$items[] = $node->title;
}
// Drupal 7
$nodes = db_select('node', 'n')->fields('n')->execute(); // или $nodes = db_query("SELECT * FROM {node}");
foreach ($nodes as $node) {
$items[] = $node->title;
}
$nodes = db_query("SELECT * FROM {node}");
while ($node = db_fetch_object($nodes)) {
$items[] = $node->title;
}
// Drupal 7
$nodes = db_select('node', 'n')->fields('n')->execute(); // или $nodes = db_query("SELECT * FROM {node}");
foreach ($nodes as $node) {
$items[] = $node->title;
}
Обновление записи:
// Drupal 6
db_query("UPDATE {node} SET status = %d WHERE nid = %d", 1, 123);
// Drupal 7, вариант 1
db_query("UPDATE {node} SET status = :status WHERE nid = :nid", array(':status' => 1, ':nid' => 123));
// Drupal 7, вариант 2 (более правильный)
db_update('node')
->fields(array('status' => 1))
->condition('nid', 123)
->execute();
//Drupal 7 плюс единица
db_update('warrior_members')
->expression('points', 'points + 1')
->condition('uid', $liv_user)
->execute();
// или задаем 1 динамически
db_update('warrior_members')
->expression('points', 'points + :amount', array(':amount' => 1))
->condition('uid', $liv_user)
->execute();
db_query("UPDATE {node} SET status = %d WHERE nid = %d", 1, 123);
// Drupal 7, вариант 1
db_query("UPDATE {node} SET status = :status WHERE nid = :nid", array(':status' => 1, ':nid' => 123));
// Drupal 7, вариант 2 (более правильный)
db_update('node')
->fields(array('status' => 1))
->condition('nid', 123)
->execute();
//Drupal 7 плюс единица
db_update('warrior_members')
->expression('points', 'points + 1')
->condition('uid', $liv_user)
->execute();
// или задаем 1 динамически
db_update('warrior_members')
->expression('points', 'points + :amount', array(':amount' => 1))
->condition('uid', $liv_user)
->execute();
Удаление записи:
// Drupal 6
db_query("DELETE FROM {node} WHERE uid = %d AND created < %d", 1, time() - 3600);
// Drupal 7, вариант 1
db_query("DELETE FROM {node} WHERE uid = :uid AND created < :created", array(':uid' => 1, ':created' => time() - 3600));
// Drupal 7, вариант 2 (более правильный)
db_delete('node')
->condition('uid', 1)
->condition('created', time() - 3600, '<')
->execute();
db_query("DELETE FROM {node} WHERE uid = %d AND created < %d", 1, time() - 3600);
// Drupal 7, вариант 1
db_query("DELETE FROM {node} WHERE uid = :uid AND created < :created", array(':uid' => 1, ':created' => time() - 3600));
// Drupal 7, вариант 2 (более правильный)
db_delete('node')
->condition('uid', 1)
->condition('created', time() - 3600, '<')
->execute();
Добавление записи:
// Drupal 6
db_query("INSERT INTO {mytable} (intvar, stringvar, floatvar) VALUES (%d, '%s', %f)", 5, 'hello world', 3.14);
$id = db_last_insert_id();
// Drupal 7
$id = db_insert('mytable')
->fields(array('intvar' => 5, 'stringvar' => 'hello world', 'floatvar' => 3.14))
->execute();
db_query("INSERT INTO {mytable} (intvar, stringvar, floatvar) VALUES (%d, '%s', %f)", 5, 'hello world', 3.14);
$id = db_last_insert_id();
// Drupal 7
$id = db_insert('mytable')
->fields(array('intvar' => 5, 'stringvar' => 'hello world', 'floatvar' => 3.14))
->execute();
Для добавления данных можно по прежнему пользоваться drupal_write_record()
Использование логического оператора OR в условии:
// Drupal 6
$nodes = db_query("SELECT * FROM {node} WHERE uid = %d OR status = %d", 1, 0);
// Drupal 7, вариант 1
$nodes = db_query("SELECT * FROM {node} WHERE uid = :uid OR status = :status", array(':uid' => 1, ':status' => 0));
// Drupal 7, вариант 2 (более правильный)
$nodes = db_select('node', 'n')
->fields('n')
->condition(
db_or()
->condition('uid', 1)
->condition('status', 0)
)
->execute();
$nodes = db_query("SELECT * FROM {node} WHERE uid = %d OR status = %d", 1, 0);
// Drupal 7, вариант 1
$nodes = db_query("SELECT * FROM {node} WHERE uid = :uid OR status = :status", array(':uid' => 1, ':status' => 0));
// Drupal 7, вариант 2 (более правильный)
$nodes = db_select('node', 'n')
->fields('n')
->condition(
db_or()
->condition('uid', 1)
->condition('status', 0)
)
->execute();
Сложные условия в WHERE:
// Drupal 6, Drupal 7
$nodes = db_query("SELECT * FROM {node} WHERE YEAR(FROM_UNIXTIME(created)) = %d", 2011);
// Drupal 7, вариант 1
$nodes = db_query("SELECT * FROM {node} WHERE YEAR(FROM_UNIXTIME(created)) = :created", array(':created' => 2011));
// Drupal 7, вариант 2 (более правильный)
$nodes = db_select('node', 'n')
->fields('n')
->where('YEAR(FROM_UNIXTIME(created)) = :created', array(':created' => 2011))
->execute();
$nodes = db_query("SELECT * FROM {node} WHERE YEAR(FROM_UNIXTIME(created)) = %d", 2011);
// Drupal 7, вариант 1
$nodes = db_query("SELECT * FROM {node} WHERE YEAR(FROM_UNIXTIME(created)) = :created", array(':created' => 2011));
// Drupal 7, вариант 2 (более правильный)
$nodes = db_select('node', 'n')
->fields('n')
->where('YEAR(FROM_UNIXTIME(created)) = :created', array(':created' => 2011))
->execute();
Сортировка
// Drupal 6, Drupal 7
$nodes = db_query("SELECT * FROM {node} ORDER BY created DESC, title ASC");
// Drupal 7, вариант 2 (более правильный)
$nodes = db_select('node', 'n')
->fields('n')
->orderBy('n.created', 'DESC')
->orderBy('n.title', 'ASC')
->execute();
$nodes = db_query("SELECT * FROM {node} ORDER BY created DESC, title ASC");
// Drupal 7, вариант 2 (более правильный)
$nodes = db_select('node', 'n')
->fields('n')
->orderBy('n.created', 'DESC')
->orderBy('n.title', 'ASC')
->execute();
Получить одну колонку из таблицы в виде массива:
// Drupal 6
$result = db_query("SELECT nid FROM {node}");
$nids = array();
while ($row = db_fetch_object($result)) {
$nids[] = $row->nid;
}
// Drupal 7, вариант 1
$nids = db_query("SELECT nid FROM {node}")->fetchCol();
// Drupal 7, вариант 2 (более правильный)
$nids = db_select('node', 'n')
->fields('n', array('nid'))
->execute()
->fetchCol();
$result = db_query("SELECT nid FROM {node}");
$nids = array();
while ($row = db_fetch_object($result)) {
$nids[] = $row->nid;
}
// Drupal 7, вариант 1
$nids = db_query("SELECT nid FROM {node}")->fetchCol();
// Drupal 7, вариант 2 (более правильный)
$nids = db_select('node', 'n')
->fields('n', array('nid'))
->execute()
->fetchCol();
Between:
$query->condition('field', array(5, 10), 'BETWEEN');
SUM
$query = db_select('node', 'n')
->fields('n', array('nid'));
$query->addExpression('SUM(likes)', 'likes');
->fields('n', array('nid'));
$query->addExpression('SUM(likes)', 'likes');
PLUS One
db_update('node')
->expression('myfield', 'myfield + :newstring', array(':newstring' => 'Some string'))
->execute();
->expression('myfield', 'myfield + :newstring', array(':newstring' => 'Some string'))
->execute();