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

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站