在drupal 7中以编程方式插入节点字段值

时间:2012-01-25 08:03:53

标签: drupal-7 nodes

我正在尝试从自定义模块插入节点字段的值。该值只会从该模块插入,然后不会执行该节点的操作,因此不考虑钩子。我试图直接将值插入field_data _...和field_revision _...表。但我发现drupal还将field_config和field_config_instance表中的值(序列化)保存为blob。因为我只在两个表中插入值,所以drupal不会读取我为节点插入的值。我无法理解存储在DB中的序列化。所以我正在寻找一种方法,可以帮助我通过API或任何其他整洁的方式插入值。

对我想要完成的事情的任何帮助都会很棒。谢谢

编辑: 在查看了field_config表的序列化内容后,我了解到序列化数据只是字段配置,并在第一次保存时插入到表中。通过admin / content保存第一个值解决了我的问题,现在我的直接数据库插入数据可供节点使用。这是我得到的序列化数据:

a:7:
   {s:12:"translatable";
    s:1:"0";
    s:12:"entity_types";
    a:0:{}
    s:8:"settings";
    a:3:
       {s:9:"precision";
        s:2:"10";s:5:"scale";
        s:1:"2";
        s:17:"decimal_separator";
        s:1:",";
       }
    s:7:"storage";
    a:5:
       {s:4:"type";
        s:17:"field_sql_storage";
        s:8:"settings";
        a:0:{}
        s:6:"module";
        s:17:"field_sql_storage";
        s:6:"active";
        s:1:"1";
        s:7:"details";
        a:1:
           {s:3:"sql";
            a:2:
               {s:18:"FIELD_LOAD_CURRENT";
                a:1:
                   {s:22:"field_data_field_total";
                    a:1:
                       {s:5:"value";
                        s:17:"field_total_value";
                       }
                   }
                s:19:"FIELD_LOAD_REVISION";
                a:1:
                   {s:26:"field_revision_field_total";
                    a:1:
                       {s:5:"value";
                        s:17:"field_total_value";
                       }
                   }
               }
          }
     }
     s:12:"foreign keys";
     a:0:{}
     s:7:"indexes";
     a:0:{}
     s:2:"id";
     s:2:"34";
    }

1 个答案:

答案 0 :(得分:3)

希望这会对你有所帮助。

$node = new stdClass();
$node->uid = 1;
$node->name = 'admin';
$node->type = 'page';
$node->language = 'und';
$node->title = 'Your title';
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->created = timestamp;
$node->field_description = array(
    'und' => array(
        array(
            'value' => 'asdasd'
        )
    )
);
$node->nid = 1; // define nid if you wish to update existing node
// if you wouldn't define $node->nid then new node would be created,
// otherwise node would be updated with you data provided for all
// fields which you'll list here.

...
// other node's fields

node_save_action($node);