Strange variable overwrite in foreach loop

2019-09-07 01:36发布

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.

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.

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.

1条回答
Anthone
2楼-- · 2019-09-07 02:06

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
        )
    );
查看更多
登录 后发表回答