如何使用 Drupal 的 node_load() 防止无限递归?
我正在使用 Ubercart 产品和 Product_kit 模块。这非常适合从相关产品套件链接到所包含的产品,但我也想从单个产品链接到它可能所属的任何套件。
我想我可以对 SKU/型号进行数据库搜索(轻松完成该部分),然后使用 node_load($nid) 获取相关套件。
到目前为止我已经做到了:
function amh_shop_nodeapi(&$node, $op, $a3 = null, $a4 = null)
{
if ($node->type == 'product') {
if ($op == 'load') {
error_log("product::load");
$bundles = array();
$results = db_query('SELECT DISTINCT n.nid FROM {node} n RIGHT JOIN {uc_products} up ON up.nid = n.nid WHERE up.model LIKE "%s /%" OR up.model LIKE "%/ %s /%" OR up.model LIKE "%/ %s"', $node->model, $node->model, $node->model);
while ($bundle = db_fetch_object($results)) {
error_log("bundle::load");
$bundles[] = node_load($bundle->nid);
}
}
}
}
但是,因为产品套件也在加载产品,所以我最终陷入了递归循环。
我想我的问题实际上分为两部分:
作为这篇文章标题的问题:如何防止这种递归?
无论如何,可能回答第一个问题的问题略有不同:我应该在加载节点时执行此操作,还是在加载过程的后期(例如,在查看或更改时)执行此
I'm using the Ubercart product and product_kit modules. This is great at linking from the relevant product kit to the products which are included, but I also want to link from the individual product to any kits it may be part of.
I figured I could do a database search on the SKU/Model number (got that part done easily), and then use node_load($nid) to get the related kit.
I have this so far:
function amh_shop_nodeapi(&$node, $op, $a3 = null, $a4 = null)
{
if ($node->type == 'product') {
if ($op == 'load') {
error_log("product::load");
$bundles = array();
$results = db_query('SELECT DISTINCT n.nid FROM {node} n RIGHT JOIN {uc_products} up ON up.nid = n.nid WHERE up.model LIKE "%s /%" OR up.model LIKE "%/ %s /%" OR up.model LIKE "%/ %s"', $node->model, $node->model, $node->model);
while ($bundle = db_fetch_object($results)) {
error_log("bundle::load");
$bundles[] = node_load($bundle->nid);
}
}
}
}
But, because the product kits are also loading the products, I end up in a recursive loop.
I guess my question actually comes in two parts:
The question that is the title of this post: how do I prevent this recursion?
Slightly different question that probably answers the first one anyway: should I be doing this when the node is being loaded, or later in the process (for example, at view or alter)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嘿,有一个名为 http://drupal.org/project/contemplate 的模块。他们在该模块中遇到了类似的递归问题,但他们通过设置 recursion_limit 找到了解决方法。
我不确定它是否能解决您的问题,但绝对值得浏览他们的模块并搜索代码以查找contemplate_max_recursion_depth。这可能会给你一些指示。
希望有帮助..
Hey there is a module called http://drupal.org/project/contemplate. They had a similar problem of recursion within that module, but they figured out a workaround for it by setting a recursion_limit.
I am not sure if it solves your problem, but it will definitely be worth going through their module and search the code for contemplate_max_recursion_depth. That might give you some pointers.
Hope it helps..
解决方案之一,就是不要对product_kit调用node_load,这样子产品就不会调用node_load,通过db_query手动查询需要的数据:
One of solutions, just don't call node_load for product_kit, so it will not call node_load for subproducts, query need datas manually via db_query:
怎么样:它引入了递归变量检查。
What about this: It introduces a recursion variable check.
你应该考虑像这样缓存数据。如果你一直加载它,它真的会耗尽资源。我会考虑一个自定义模块,它可以存储各个项目和关联套件之间的关系。
you should consider cacheing data like this. If you're loading it all the time it could really drain resources. I would consider a custom module that can store a relationship between individual projects and associated kits.