• 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);
    }
    
  • 相关阅读:
    求一个二维数组的最大子矩阵
    在一整型数组中找到此数组中子数组和的最大值
    软件工程个人小项目:写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来
    Redis 为什么这么快?
    在netfarmerwork3.5版本的winform下执行string串中的代码
    c# 反射(Reflection)详解
    string,特殊的引用类型
    c#使用HashSet<T>集合去重
    c# .Net重试机制
    观察者模式
  • 原文地址:https://www.cnblogs.com/catcat811/p/2994514.html
Copyright © 2020-2023  润新知