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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀