简体   繁体   English

在foreach循环中奇怪的变量被覆盖

[英]Strange variable overwrite in foreach loop

This must be something ugly obvious, but I'm stucked on this and can't solve it for past two hours. 这一定是显而易见的,但是我一直坚持下去,并且在过去的两个小时中无法解决。

I have this piece of code: 我有这段代码:

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 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. echo部分按预期工作(每个项目的$pageId不同,取自$idMap ),而Yii的CDbCommand::update()获取wako并使$pageId等于所有循环迭代的最后一个值。

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. 换句话说,如果我有20个菜单项,并且结果集中的最后一个项具有pageId = 18 ,那么在使用CDbCommand::update() ,我会将所有菜单项都设置为该最后一个值。

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. 这里肯定有一些变量覆盖,但是我在过去的两个小时内找不到它,尤其是, echo仅放在上面一行就可以了。 Can someone help here. 有人可以帮忙吗?

Guessing, but does $this->update() expect a single array for its bind arguments? 猜测,但是$this->update()期望其绑定参数使用单个数组?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM