How to create a query in Drupal 8(如何在 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_select 或 db_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 中创建查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Drupal 8 中创建查询
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
