简体   繁体   中英

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.

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.

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
        )
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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