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)
$nids = db_select('node', 'n')->fields('n', array('nid'))->execute()->fetchField(); // return 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');
echo (string)$query; // 打印SQL
$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();
关联field查询
$user_select = db_select('users', 'u')->condition('uid', 0, '<>');
$user_select->leftJoin('field_data_field_city', 'fdfc', '(fdfc.entity_id = u.uid AND fdfc.entity_type = :entity_type AND fdfc.bundle = :bundle)', array(':entity_type' => 'profile2', ':bundle' => 'main'));
$user_select->fields('fdfc', array('field_city_value'));
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();
db_select('node', 'n')->countQuery()->execute()->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);
Query Tags
$query = db_select('node','n');
$query->addTag('my_tag');
$tid = $query->execute()->fetchCol();
function mymodule_query_alter(SelectQueryInterface $query) {
if($query->hasTag('my_tag')) {
$query->addField('d','vid');
}
}
Query Tags - 以node数量排序并排除没有node的
if($query->hasTag('term_order_by_nodes')) {
$query->join('taxonomy_index', 't', 't.tid = taxonomy_term_data.tid');
$query->groupBy('taxonomy_term_data.tid');
$query->having('COUNT(*) > 0');
$query->orderBy('COUNT(*)', 'DESC');
}
写入数据
db_insert('password_data')
->fields(array(
'username' => $d['username'],
'password' => base64_encode($encrypted)
))
->execute();
官方API文档: Link
EntityFieldQuery
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', NODE_PUBLISHED)
->fieldCondition('field_news_types', 'value', 'spotlight', '=')
// See the comment about != NULL above.
->fieldCondition('field_photo', 'fid', 'NULL', '!=')
->fieldCondition('field_faculty_tag', 'tid', $value)
->fieldCondition('field_news_publishdate', 'value', $year . '%', 'like')
->fieldOrderBy('field_photo', 'fid', 'DESC')
->range(0, 10)
// Run the query as user 1.
->addMetaData('account', user_load(1));
$result = $query->execute();
if (isset($result['node'])) {
$items_nids = array_keys($result['node']);
$items = entity_load('node', $items_nids);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。