Sei sulla pagina 1di 2

->orderRandom() ->db_or()->condition()->condition()

SELECT
return The called query object. return Condition of OR-ed conditions.
db_select($table, $alias = NULL,
$options = array()) ->union(SelectQueryInterface $query, ->db_and()->condition()->condition()
$table Database table to select from. $type = '') return Condition of AND-ed conditions.
$alias Table alias. $query Query to union.
return New query object. $type Type of union. ->isNull($field)
return New resulting query object.
->isNotNull($field)
$query = db_select('users', 'u')
->fields('u', $field The field to check.
->addExpression($expression, $alias
array('uid', 'name')); return The called query object.
= NULL, $arguments = array())
$result = $query->execute();
$expression Expression string.
->exists(SelectQueryInterface
$alias Expression alias.
->distinct($distinct = TRUE) $select);
$arguments Assoc array of placehoders and
$distinct Flag indicating DISTINCT query. placeholder values. ->notExists(SelectQueryInterface
return The called query object. return Unique expression alias. $select);
$select The query to check.
->fields($table_alias, $fields = ->countQuery() return The called query object.
array()) return New query object.
$table_alias Alias of the table the field PAGER
JOIN
belongs to. ->addTag($tag)
$fields Array of field names. ->extend(‘PagerDefault’)
->join($table, $alias = NULL,
$tag Query identification.
return The called query object. $condition = NULL, $arguments = return New pager extender object.
return The called query object.
array())
->addField($table_alias, $field, $table The table to join with. ->extend(‘PagerDefault’)->limit
->hasTag($tag)
$alias = NULL) $alias Table alias. ($count)
$tag Query identification. $condition Join conditions.
$table_alias Alias of the table the field $count Number of items per page.
return TRUE if condition is met. $arguments Assoc array of placeholders and
belongs to.
$field Field name. placeholder values.
return Unique table alias. SORTABLE TABLE
$alias Field alias. CONDITIONS
return Unique field alias.
->extend(‘TableSort’)
->condition($field, $value = NULL, $query = db_select('users', 'u');
$query->innerJoin('node', 'n', return Table extender object.
->range($start = NULL, $length = $operator = NULL)
'n.uid = u.uid'); return The called query object.
NULL) $field The field to check or the result of $query->addField('u', 'name');
$start First record of the result set. a logic operation (or, and, xor) $query->addField('n', 'title'); ->extend(‘TableSort’)->orderByHeader
$length Max number of records. $value The value to test against. $result = $query->execute(); ($header)
return The called query object. $operator Default: ‘=’ or ‘IN’.
Supported: =, <, >, >=, <=, IN, $header Array with sorting criteria.
NOT IN, LIKE, BETWEEN, IS ->innerJoin ($table, $alias = NULL, return The called query object.
->groupBy($field) $condition = NULL, $arguments =
NULL, IS NOT NULL
$field The field to group by. return The called query object. array()) $header = array(
return The called query object. ->leftJoin ($table, $alias = NULL, array(
$condition = NULL, $arguments = 'data' => t('Title'),
->where($snippet, $args = array())
->orderBy($field, $direction = array()) 'field' => 'n.title',
$snippet Where clause (with placeholders) 'sort' => 'desc'),
'ASC') ->rightJoin ($table, $alias = NULL,
$args Assoc array of placeholders and t('Operations'),
$field The field to order by. placeholder values. $condition = NULL, $arguments =
);
$direction 'ASC' or 'DESC'. array())
return The called query object. See join method.

! March 2011! Drupal-7 database! www.wizzlern.nl/drupal/cheat-sheets


->fetchCol($index = 0) $num_updated = db_update('node')
RESULTS   ->fields(array(
$index Numeric index of the column.
return Array of all records.     'uid' => 5,
->execute($args = array(), $options     'status' => 1,
= array())   ))
return The called query object. INSERT   ->condition('created',
REQUEST_TIME - 3600, '>=')
->fetch($mode = NULL, db_insert($table, $options = array   ->execute();
$cursor_orientation = NULL, ())
$cursor_offset = NULL) $table Database table to insert into.
MERGE
$mode Fetch mode. return New query object.
return Result type specified by $mode. db_merge($table, $options = array())
$nid = db_insert('node')
  ->fields(array( $table Database table to merge into
->fetchObject($class_name = NULL,     'title' => 'Example', return New query object
$constructor_args = array())     'uid' => 1,
$class_name Class type to be returned.     'created' => REQUEST_TIME)) db_merge('role')
Default: stdClass   ->execute();   ->key(array('name' => $name))
return Object of one record.   ->fields(array(
      'weight' => $weight,
->values(array $values)   ))
->fetchAssoc() $values Assoc array of values to insert.   ->execute(); QUERIES
return Associative array of one record. return The called query object.
db_query($query, $args = array(),
->key(array $fields, $values = array
->fetchAllAssoc($key, $fetch = NULL) $nid = db_insert('node') $options = array())
())
  ->fields(array('title', 'uid', Note: Access control is not supported! Query may
$key Field name of the array key $fields Array of fields to match or set.
'created')) not be compatible with other database types.
$fetch Fetch mode Or associative array of fields and
  ->values(array(
(PDO::FETCH_ASSOC, values.
    'title' => 'Example',
PDO::FETCH_NUM, or $values Values to set.
    'uid' => 1, settings.php
PDO::FETCH_BOTH). return The called query object.
    'created' => REQUEST_TIME))
return Associative array of data objects Single database configuration example:
  ->execute();
$databases['default']['default'] =
DELETE array(
->fetchAll($mode = NULL,
->from(SelectQueryInterface $query) 'driver' => 'mysql',
$column_index = NULL, db_delete($table, $options = array
$constructor_arguments = array()) $query Select query to fetch the rows 'database' => 'databasename',
())
that should be inserted. 'username' => 'username',
$mode Fetch mode. See above.
return The called query object. $table Database table to delete from. 'password' => 'password',
return Array of data objects. Depending return New query object. 'host' => 'localhost',
on fetch mode. 'prefix' => '',
UPDATE $num_deleted = db_delete('node') 'collation' =>'utf8_general_ci',
->fetchField($index = 0)   ->condition('nid', 5) );
db_update($table, $options = array
$index Numeric index of the column.   ->execute();
())
return A single field.
$table Database table to update. DEBUGGING
return New query object. TRUNCATE
->fetchAllKeyed($key_index = 0, print($query->__toString());
$value_index = 1) db_truncate($table, $options = array
$key_index Numeric index of the array key. ())
$value_index Numeric index of the array value. DOCUMENTATION
$table Database table to remove.
return Associative array of all records. return New query object. Database API on drupal.org:
http://drupal.org/developing/api/database

! March 2011! Drupal-7 database! www.wizzlern.nl/drupal/cheat-sheets

Potrebbero piacerti anche