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 中创建查询


- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01