脚本宝典收集整理的这篇文章主要介绍了php – Drupal – 如何以编程方式更新CCK NodeReference字段?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$node_tyPE_A = node_load($some_nid); $node_type_A->field_type_B_node_ref[]['nid'] = $node_type_B_nid; $node_type_A = node_submIT($node_type_A); node_save($node_type_A);
结果,将创建一个新的B类型节点,但不会为A类型节点分配任何引用.任何帮助,将不胜感激.
达美价值
首先,您需要知道附加到$node_type_A的最新节点引用的delta值,delta实际上是部分索引,当与$node_type_A的vid字段结合使用时,它们将成为数据库中节点引用表的索引.换句话说,它是在$node_type_A中引用的$node_type_B的计数,好吗?
GApple再次正确,你必须确切地说在哪里添加新的参考.获得该delta值后,您可以准确地说出新引用的附加位置(delta 1).这里是:
function get_current_delta($node_vid){ return db_result(db_query("SELECT delta From {content_field_type_A_node_ref} WHERE vid = '%d' ORDER BY delta DESC LIMIT 1",$node_vid)); }
添加新引用
我们得到三角洲!所以我们可以将新的$node_type_B节点附加到$node_type_A节点:
// Loading type_A node. $node_type_A = node_load($some_nid); // Getting current delta value. $current_delta = get_current_delta($node_type_A->vid); // "Appending" a node reference based on delta. $node_type_A->field_type_B_node_ref += array($current_delta + 1 => array('nid' => $node_type_B_nid));
重新保存更新的节点
(可选)调用node_submit()以填充节点对象中的一些基本字段,并使用node_save()进行保存.毕竟,您需要调用content_insert()
以使节点完全保存在其CCK字段之外:
// Resaving the updated node. $node_type_A = node_submit($node_type_A); node_save($node_type_A); content_insert($node_type_A);
刷新内容缓存
可能是最重要的部分,这几天就是杀了我. CCK在数据库中有一个名为cache_content的缓存表(看一下它的结构),在重新保存更新后的节点后,你会注意到$node_type_A主题输出中没有任何变化,即使这些表已更新.我们必须从该内容缓存表中删除一条记录,这将迫使Drupal显示数据的最新快照.您可以将以下内容定义为函数:
db_query("DELETE From {cache_content} WHERE cid = '%s'",'content:' . $node_type_A->nid . ':' . $node_type_A->vid);
希望能帮助到你
以上是脚本宝典为你收集整理的php – Drupal – 如何以编程方式更新CCK NodeReference字段?全部内容,希望文章能够帮你解决php – Drupal – 如何以编程方式更新CCK NodeReference字段?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。