问题
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:
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
)
);
来源:https://stackoverflow.com/questions/29037370/strange-variable-overwrite-in-foreach-loop