如何在Drupal 8中创建查询 [英] How to create a query in Drupal 8
问题描述
我习惯于在drupal 7中使用db_select,但现在在drupal 8中已经被弃用了
所以,如果我需要创建一个查询来列出所有的用户从code> users_field_data 表,我该怎么办?
我还使用 db_select
或 db_query
即使它们是不推荐的功能?或者创建一个新的控制器,从选择类
扩展,并进行查询?
使用存储对象
如果要对用户进行简单查询,那么您应该使用存储对象的loadByProperties
$ users = \Drupal :: entityTypeManager() - > getStorage('user') - > loadByProperties([
'name'=>'bar'
]);
使用实体查询& loadMultiple
如果您需要使用各种类型,范围,寻呼机和OR / AND条件组的更复杂的查询,则应使用实体查询
$ ids = \Drupal :: entityQuery('user') - > condition('name','foo') - > ;执行();
$ users = User :: loadMultiple($ ids);
I am used to using db_select in drupal 7 but now it's deprecated in drupal 8
So, If I need to create a query to list all users from users_field_data
table, What should I do?
Do I still use db_select
or db_query
even though they are deprecated functions? Or create a new controller to extend from "Select class
" and make my query?
Depends on what you are trying to achieve.
Using the storage object
If you want to make a simple query about the users then you should use the loadByProperties of the storage object
$users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties([
'name' => 'bar'
]);
Using entity query & loadMultiple
If you need a more complex query with sorts, range, pager and OR/AND condition groups you should use entity query
$ids = \Drupal::entityQuery('user')->condition('name', 'foo')->execute();
$users = User::loadMultiple($ids);
这篇关于如何在Drupal 8中创建查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!