在foreach循环中奇怪的变量被覆盖 [英] Strange variable overwrite in foreach loop

查看:79
本文介绍了在foreach循环中奇怪的变量被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一定是丑陋的,但是我一直坚持下去,过去两个小时无法解决.

This must be something ugly obvious, but I'm stucked on this and can't solve it for past two hours.

我有这段代码:

foreach($idMap as $menuId=>$pageId)
{
    echo('$this->update("menus_items", SET "link = /content/show?id='.$pageId.'" WHERE id = '.$menuId.');'."\n");

    $this->update
    (
        'menus_items',
        array('link'=>'/content/show?id='.$pageId),
        array('id = '.$menuId)
    );
}

echo 部分按预期工作(每个项目的 $ pageId 不同,取自 $ idMap ),而Yii的 CDbCommand :: update() 得到和光和 $ pageId 等于所有循环迭代的最后一个值.

echo part works as expected ($pageId is different for each item, taken from $idMap), while Yii's CDbCommand::update() gets wako and have $pageId equal to it's last value for all loop iterations.

换句话说,如果我有20个菜单项,并且结果集中的最后一个项目具有 pageId = 18 ,则在使用 CDbCommand :: update()时,我将所有菜单项设置为最后一个值.

In other words, if I have 20 menu items and last item in result set has pageId = 18, then when using CDbCommand::update(), I'm getting all menu items set to that last value.

这里肯定有一些变量覆盖,但是我在过去的两个小时内找不到它,尤其是 echo 放在上面仅一行很有效.有人可以帮忙吗?

There must be some variable overwriting here, but I can't find it for past two hours, especially, that echo put just one line above works great. Can someone help here.

推荐答案

猜测,但是 $ this-> update()期望绑定参数使用单个数组吗?

Guessing, but does $this->update() expect a single array for its bind arguments?

$this->update
    (
        'menus_items',
        array(
            'link' => '/content/show?id='.$pageId,
            'id' => $menuId
        )
    );

这篇关于在foreach循环中奇怪的变量被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆