纳速健身

标题: drupal7数据库查询方法总结drupal8有少部分改动 [打印本页]

作者: awagink    时间: 2016-2-1 19:19
标题: drupal7数据库查询方法总结drupal8有少部分改动
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





欢迎光临 纳速健身 (https://nasue.com/) Powered by Discuz! X3.4