如何在Drupal 8中创建查询 [英] How to create a query in Drupal 8

查看:309
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆