How to create a query in Drupal 8

前端 未结 3 1887
情话喂你
情话喂你 2021-02-13 21:18

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,

3条回答
  •  遇见更好的自我
    2021-02-13 21:33

    As mentioned in the documentation you can query data by injecting Drupal's database connection class. For example:

    use Drupal\Core\Database\Connection;
    
    class DatabaseQueryExample {
    
      protected $connection;
    
      public function __construct(Connection $connection) {
        $this->connection = $connection;
      }
    
      public function queryExamples() {
        // db_query()
        $this->connection->query(" ... ");
        // db_select()
        $this->connection->select(" ... ");   
      }
    
    }
    

提交回复
热议问题