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

查看:25
本文介绍了如何在 Drupal 8 中创建查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于在 drupal 7 中使用 db_select 但现在它在 drupal 8 中被弃用

I am used to using db_select in drupal 7 but now it's deprecated in drupal 8

那么,如果我需要创建一个查询来列出 users_field_data 表中的所有用户,我该怎么办?

So, If I need to create a query to list all users from users_field_data table, What should I do?

我是否仍然使用 db_selectdb_query 即使它们是不推荐使用的函数?或者创建一个新的控制器来扩展Select class"并进行查询?

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.

如果你想对用户做一个简单的查询,那么你应该使用存储对象的 loadByProperties

$users = Drupal::entityTypeManager()->getStorage('user')->loadByProperties([
  'name' => 'bar'
]);

<小时>

使用实体查询&加载多个

如果您需要更复杂的查询,包括排序、范围、分页和 OR/AND 条件组,您应该使用实体查询

$ids = Drupal::entityQuery('user')->condition('name', 'foo')->execute();
$users = User::loadMultiple($ids);

这篇关于如何在 Drupal 8 中创建查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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