Drupal7 常用的数据读写API

// user load & change
user_load($uid, $reset = FALSE);
user_load_multiple($uids = array(), $conditions =array(), $reset = FALSE);
$user = user_load($uid);
$user->name = 'xxxx';
user_save($user);

// menu tree
menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL);
menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = FALSE);

// term
taxonomy_term_load($tid) : object
taxonomy_term_load_multiple($tids = array(), $conditions = array()) : array
 
// get more terms
$tree = taxonomy_get_tree($vid, $parent = 0, $max_depth);
foreach($tree as $leaf) {
    print $leaf->tid. $leaf->vid. $leaf->name. implode(',', $leaf->parents);
}
 
// create term
$term = new stdClass();
$term->vid = 1;
$term->name = 'test';
$term->language = LANGUAGE_NONE;
taxonomy_term_save($term);

// block
block_load($module, $delta);

// Pager
db_select('node', 'n')
    ->extend('PagerDefault')->limit(5)
    ->fields('n');
$statement->fetchField();
db_query_range('SELECT n.nid, n.title, n.created
  FROM {node} n WHERE n.uid = :uid', 0, 10, array(':uid'=> $uid));

// insert
$fields =array('nid'=> 1, 'title' =>'my title', 'body'=> 'my body');
db_insert('node')->fields($fields)->execute();

// update
db_update('example')
  ->condition('id', $id)
  ->fields(array('field2' => 10))
  ->execute();

// select
$query = db_select('comment', 'c')
  ->fields('c', array('subject', 'name'))
  ->fields('n', array('title'))
  ->extend('PagerDefault')->limit(5)
  ->condition('n.type', array('article'), 'IN')
  ->orderBy('c.cid', 'DESC');
$query->join('node', 'n', 'n.nid = c.nid');
$rows = $query->execute()->fetchAllAssoc();
 
foreach($query->execute() as $row) {print $row->nid;}
 
$query = db_select('node', 'n')->fields('n', array('title'))->distinct();
$query->join('taxonomy_index', 't', 't.nid = n.nid');
$or= db_or()->condition('n.uid', $authorId)->condition('t.tid', $cats, 'IN');
$query->condition($or)->execute()->fetchCol();
 
// return array
$row = db_select('node', 'n')->condition('n.nid', 1)->execute()->fetchAssoc();
 
// delete
db_delete('uc_products')->condition('nid', $nid)->execute();
 
// range select
$nids = db_query_range("SELECT nid FROM {node} WHERE nid > :nid", 0, $limit, array(':nid'=> 1))->fetchCol();
 
// select count(*)
db_query('SELECT COUNT(*) FROM {node} WHERE created > ?', array($created))->fetchField();

// fetch
foreach ($query->execute() as $object) {
  echo $object->name;
}
 
// list user by role
  $query = db_select('users', 'u')
    ->fields('u', array('uid'))
    ->condition('ur.rid', $role_id)
    ->condition('status', 1);
  $query->join('users_roles', 'ur', 'ur.uid = u.uid');
  $uids = $query->execute()->fetchCol();
 
// change user role
$uid = 123;// User ID of user that you want to add role to.
$role_name = 'Role to add'; // The name of the role to add.
if ($role = user_role_load_by_name($role_name)) {
  user_multiple_role_edit(array($uid), 'add_role', $role->rid);
}

 

posted @ 2013-04-01 23:06  猫之良品  阅读(2530)  评论(0编辑  收藏  举报