立即保存以编程方式嵌套的分类术语

发布于 2024-10-08 02:39:42 字数 725 浏览 5 评论 0原文

下面的代码允许使用 Drupal API 创建分类术语

$terms = array(   

      $term1 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),

      $term2 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),

      $term3 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),
);

foreach ($terms as $term) {
$term = (object) $term;
taxonomy_term_save($term);

}

它对于同级术语效果很好,但是如果我需要创建嵌套分类树怎么办?有一个“父”键应该包含父术语 ID 数组来执行此操作,

在将父术语保存到数据库中之前,我如何知道这些 ID?

the code below allows to create taxonomy terms using Drupal API

$terms = array(   

      $term1 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),

      $term2 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),

      $term3 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
       ),
);

foreach ($terms as $term) {
$term = (object) $term;
taxonomy_term_save($term);

}

It working well for sibling terms but what if I need to create nester taxonomy tree? There is 'parent' key that should contain array of parent term ids to do that

How will I know these IDs before parent terms get saved in DB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2024-10-15 02:39:42

该密钥通过 taxonomy_term_save 添加到传递的术语对象中(更具体地说,通过 taxonomy_term_save 调用 drupal_write_record):

$term1 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
);
$term1 = (object) $term1;
taxonomy_term_save($term1);
echo $term1->tid; // now where did that come from?

The key is added to the passed term object by taxonomy_term_save (more specifically by taxonomy_term_save calling drupal_write_record):

$term1 = array(
      'name' => 'term name', 
      'description' => '', 
      'parent' => array(0), 
      'vid' => $vid,
);
$term1 = (object) $term1;
taxonomy_term_save($term1);
echo $term1->tid; // now where did that come from?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文