Yii Framework Forum: CDbMigration::update does not work inside foreach loop - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CDbMigration::update does not work inside foreach loop Rate Topic: -----

#1 User is offline   Trejder 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,254
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 13 March 2015 - 04:38 PM

It seems, that I was too quick in deleting my previous post. There is something wrong, when using CDbMigration::update() inside foreach loop.

This code does not work correctly:

//This is executed inside Yii migration, so $this is CDbMigration.

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

For each item in $idMap value of $pageId is always the same and equals value of last item in $idMap array. Therefore, every menu item points to the same URL.

This code works like a charm:

foreach($idMap as $menuId=>$pageId)
{
	$sql = "UPDATE `menus_items` SET link = '/content/show?id=".$pageId."' WHERE id = ".$menuId."; ";

	Yii::app()->db->createCommand($sql)->execute();
}

For each item in $idMap value of $pageId is always different and equals value of current item in $idMap array. Therefore, every menu item points to correct URL.

The same goes, when executing all statements in one SQL query:

$sql = '';

foreach($idMap as $menuId=>$pageId)
{
	$sql .= "UPDATE `menus_items` SET link = '/content/show?id=".$pageId."' WHERE id = ".$menuId."; ";
}

Yii::app()->db->createCommand($sql)->execute();

Again, everything is OK.

What am I missing? Why direct SQL execution works like a charm, while using CDbMigration::update() fails?
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#2 User is offline   Trejder 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,254
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 14 March 2015 - 08:15 AM

Answered at Stack Overflow. There's a bug in my call to CDbMigrate::update, parameters section should be given as string or array and I mixed both approaches.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users