THELIA Forum

Welcome to the THELIA support and discusssion forum

Announcement

Rejoignez la communauté sur le Discord Thelia : https://discord.gg/YgwpYEE3y3

Offline


After importing a Thelia 1 database to a Thelia 2.2.1 platform,
I've been trying to apply a php script to do some cleaning.

Especially, I've been trying to move the products from the imported categories to new ones.

The products are removed from the old category, I can see that in the backoffice,
but the new category is still empty. Apparently something is missing in my script.

basically, what I do is :

      $product->addCategory($category);
      $product->removeCategory($t1Category);

      $productCategories = $product->getProductCategories();
      $productCategories[0]->setDefaultCategory(1);
      $productCategories->save();

      $product->save();

Does anybody would have an idea about what I'm missing ?

thanks for the help,
loïc b.

Last edited by neywen (04-01-2016 15:33:02)

Offline


Is $productCategories[0] containing the expected (e.g., new one) category ?

Try to save the category changes befor requesting category list :

$product->addCategory($category)->removeCategory($t1Category)->save();

$productCategories = $product->getProductCategories();

OpenStudio Toulouse

Offline


Thanks for your message,

I've enumerated the ProductCategory objects from the collection,
and I can see that it contains both the old one and the new one :

my logs :

// log : move product 20 from category 11 to category 35

$product->addCategory($category);
$product->removeCategory($t1Category);

$productCategories = $product->getProductCategories();
foreach ($productCategories as $productCategory) {
    echo "enumerated productCategory ".$productCategory->getCategoryId()."\n";
 }

// log :
// enumerated productCategory 11
// enumerated productCategory 35

Does it mean that the removeCategory call did not properly worked ?
l.

[EDIT] : I just saw your edit. I'm going to try now.

Last edited by neywen (04-01-2016 15:53:06)

Offline


It does not change anything.
Both old and new categories are still enumerated when I call getProductCategories().

I'm going to try to set a new product categories collection.

      $collection = new \Propel\Runtime\Collection\Collection();
      $collection->prepend($productCategories[1]->setDefaultCategory(1));
      $product->setProductCategories($collection);

Yep. Saving the product after add/removeCategory and setting a new ProductCategories collection does the expected job.
Thank you for your help.

I'm just concerned about why the old category is still enumerated after calling removeCategory. If removeCategory is not supposed to dynamically update the productCategories collection, then that's ok, it's the appropriate behavior. otherwise, it might indicate another problem somewhere, and then, my "$productCategories[1]->setDefaultCategory(1)" is a bit hacky. But it's good enough for what I need to do.

Thanks !
loïc b.

Last edited by neywen (04-01-2016 16:05:17)