返回顶部

[文摘] drupal7数据库查询方法总结drupal8有少部分改动

[复制链接]
awagink 显示全部楼层 发表于 2016-2-1 19:19:37 |阅读模式 打印 上一主题 下一主题
user load & change
  1. user_load($uid, $reset = FALSE);
  2. user_load_multiple($uids = array(), $conditions =array(), $reset = FALSE);
  3. $user = user_load($uid);
  4. $user->name = 'xxxx';
  5. user_save($user);
复制代码

menu tree
  1. menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL);
  2. menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = FALSE);
复制代码

term
  1. taxonomy_term_load($tid) : object
  2. taxonomy_term_load_multiple($tids = array(), $conditions = array()) : array
复制代码

get more terms
  1. $tree = taxonomy_get_tree($vid, $parent = 0, $max_depth);
  2. foreach($tree as $leaf) {
  3.     print $leaf->tid. $leaf->vid. $leaf->name. implode(',', $leaf->parents);
  4. }
复制代码

create term
  1. $term = new stdClass();
  2. $term->vid = 1;
  3. $term->name = 'test';
  4. $term->language = LANGUAGE_NONE;
  5. taxonomy_term_save($term);
复制代码

block
  1. block_load($module, $delta);
复制代码

Pager
  1. db_select('node', 'n')
  2.     ->extend('PagerDefault')->limit(5)
  3.     ->fields('n');
  4. $statement->fetchField();
  5. db_query_range('SELECT n.nid, n.title, n.created
  6.   FROM {node} n WHERE n.uid = :uid', 0, 10, array(':uid'=> $uid));
复制代码

insert
  1. $fields =array('nid'=> 1, 'title' =>'my title', 'body'=> 'my body');
  2. db_insert('node')->fields($fields)->execute();
复制代码

update
  1. db_update('example')
  2.   ->condition('id', $id)
  3.   ->fields(array('field2' => 10))
  4.   ->execute();
复制代码

select
  1. $rows = db_select('node', 'n')->fields('n')->execute()->fetchAllAssoc('nid'); // return array(object)
  2. $row = db_select('node', 'n')->fields('n')->execute()->fetchAssoc(); // return array
  3. $nids = db_select('node', 'n')->fields('n', array('nid'))->execute()->fetchCol(); // return array(int)


  4. $query = db_select('comment', 'c')
  5.   ->fields('c', array('subject', 'name'))
  6.   ->fields('n', array('title'))
  7.   ->extend('PagerDefault')->limit(5)
  8.   ->condition('n.type', array('article'), 'IN')
  9.   ->orderBy('c.cid', 'DESC');
  10. $query->join('node', 'n', 'n.nid = c.nid');
  11. $rows = $query->execute()->fetchAllAssoc('cid');

  12. foreach($query->execute() as $row) {print $row->nid;}

  13. $query = db_select('node', 'n')->fields('n', array('title'))->distinct();
  14. $query->join('taxonomy_index', 't', 't.nid = n.nid');
  15. $or= db_or()->condition('n.uid', $authorId)->condition('t.tid', $cats, 'IN');
  16. $query->condition($or)->execute()->fetchCol();
复制代码

delete
  1. db_delete('uc_products')->condition('nid', $nid)->execute();
  2. range select
  3. $nids = db_query_range("SELECT nid FROM {node} WHERE nid > :nid", 0, $limit, array(':nid'=> 1))->fetchCol();
  4. select count(*)
  5. db_query('SELECT COUNT(*) FROM {node} WHERE created > ?', array($created))->fetchField();
  6. fetch
  7. foreach (db_select('node', 'n')->execute() as $row) {
  8.   echo $row->nid;
  9. }

  10. foreach (db_query("SELECT nid FROM {node}") as $row) {
  11.   dpm($row->nid);
  12. }
复制代码

list user by role
  1.   $query = db_select('users', 'u')
  2.     ->fields('u', array('uid'))
  3.     ->condition('ur.rid', $role_id)
  4.     ->condition('status', 1);
  5.   $query->join('users_roles', 'ur', 'ur.uid = u.uid');
  6.   $uids = $query->execute()->fetchCol();
复制代码

change user role
  1. $uid = 123;// User ID of user that you want to add role to.
  2. $role_name = 'Role to add'; // The name of the role to add.
  3. if ($role = user_role_load_by_name($role_name)) {
  4.   user_multiple_role_edit(array($uid), 'add_role', $role->rid);
  5. }
复制代码

user额外属性
user与node一样可以添加field,不过field是比较复杂的结构,如果所需属性并不复杂并且不需要系统管理,只用于代码存储状态用途,可以使用user对象中的data属性。
  1. $user = user_load(1);
  2. $user->data['field_xxx'] = true;
  3. user_save($user);
复制代码

读取profile2数据(像node一样操作)
  1. $profile = profile2_load_by_user($user, 'merchant');
  2. $profile->field_image[LANGUAGE_NONE][0];
复制代码

flag module - 得到user所有flagged的对象
  1. flag_get_user_flags('node', NULL, $uid);
  2. flag module - 得到被flag的所有user对象
  3. flag_get_content_flags('node', $nid);
复制代码

官方API文档: Link原文:http://www.tuicool.com/articles/bAjaae
您需要登录后才可以回帖 登录 | 注册

本版积分规则

纳速健身网成立于2006年8月,是国内优秀健身运动网站,现拥浏览人数超30万。网站是集养生、武术、太极拳和健身气功等多种健身项目于一体的多功能交流平台。平台提供大量优质的教学视频、伴奏音乐(太极拳晨练音乐,广场舞音乐,健身气功音乐)、图文教程、运动科普和经验分享,为健身爱好者提供完善的运动指导平台。
  • 纳速QQ群乙:151815303
  • 纳速QQ群丙:79104490
  • 微信交流群:微信好友搜索【nasuwang】加小纳微信进群交流健身知识,备注【纳速】
  •                     或者扫描页面底部右侧二维码添加小纳微信>>>
  • 微信公众号

  • 微信群客服交流

  • Copyright © 2006-2021, 纳速健身网. | | 辽ICP备13002388号-1 辽公安网备21050202000005号公安网备号 纳速武术-乙 QQ