纳速健身
标题:
drupal7数据库查询方法总结drupal8有少部分改动
[打印本页]
作者:
awagink
时间:
2016-2-1 19:19
标题:
drupal7数据库查询方法总结drupal8有少部分改动
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
$rows = db_select('node', 'n')->fields('n')->execute()->fetchAllAssoc('nid'); // return array(object)
$row = db_select('node', 'n')->fields('n')->execute()->fetchAssoc(); // return array
$nids = db_select('node', 'n')->fields('n', array('nid'))->execute()->fetchCol(); // return array(int)
$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('cid');
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();
复制代码
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 (db_select('node', 'n')->execute() as $row) {
echo $row->nid;
}
foreach (db_query("SELECT nid FROM {node}") as $row) {
dpm($row->nid);
}
复制代码
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);
}
复制代码
user额外属性
user与node一样可以添加field,不过field是比较复杂的结构,如果所需属性并不复杂并且不需要系统管理,只用于代码存储状态用途,可以使用user对象中的data属性。
$user = user_load(1);
$user->data['field_xxx'] = true;
user_save($user);
复制代码
读取profile2数据(像node一样操作)
$profile = profile2_load_by_user($user, 'merchant');
$profile->field_image[LANGUAGE_NONE][0];
复制代码
flag module - 得到user所有flagged的对象
flag_get_user_flags('node', NULL, $uid);
flag module - 得到被flag的所有user对象
flag_get_content_flags('node', $nid);
复制代码
官方API文档:
Link
原文:
http://www.tuicool.com/articles/bAjaae
欢迎光临 纳速健身 (https://nasue.com/)
Powered by Discuz! X3.4