dongyan3616 2015-11-24 15:43
浏览 165
已采纳

如何在Drupal 8中从数据库中写入和读取信息?

I can't write and read informations from the database in Drupal 8.

In Drupal 7 it looked like this:

# save & update
variable_set('variable', 'value');

# get
variable_get('test', false);

I know that in Drupal 8 it looks completely different but enerything I tried does not work.

  • 写回答

1条回答 默认 最新

  • duanping6698 2015-11-28 03:09
    关注

    To read data from a configuration object use \Drupal::config()->get('system.site')->get('name').

    To set data to a configuration object you first need to load the editable configuration entity, then set the data and finally save it.

    $site_settings = \Drupal::configFactory()->getEditable('system.site');
    $site_settings->set('name', 'Foo site');
    $site_settings->save();
    

    To save custom configuration to the database, create a configuration yaml file:

    modules/MODULE_NAME/config/install/custom.configuration.yml

    custom-variable: 'hello world'
    

    You should also probide a schema file for your configurations:

    modules/MODULE_NAME/config/schema/custom.configuration.schema.yml

    custom-variable:
      type: 'string'
      label: 'Custom variable'
      description: 'A custom variable to store information in the database'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?