Skip to content

Commit

Permalink
Implements saving referenced items too
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Apr 21, 2018
1 parent f6505ae commit 246d8b6
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/mshoplib/src/MShop/Attribute/Manager/Standard.php
Expand Up @@ -247,7 +247,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
}

if( !$item->isModified() ) {
return $item;
return $this->saveRefItems( $item, 'attribute' );
}

$context = $this->getContext();
Expand Down Expand Up @@ -408,7 +408,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'attribute' );
}


Expand Down
8 changes: 6 additions & 2 deletions lib/mshoplib/src/MShop/Catalog/Manager/Standard.php
Expand Up @@ -406,7 +406,7 @@ public function insertItem( \Aimeos\MShop\Catalog\Item\Iface $item, $parentId =
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'catalog' );
}


Expand Down Expand Up @@ -453,6 +453,10 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw new \Aimeos\MShop\Catalog\Exception( sprintf( 'Object is not of required type "%1$s"', $iface ) );
}

if( !$item->isModified() ) {
return $this->saveRefItems( $item, 'catalog' );
}

$siteid = $this->getContext()->getLocale()->getSiteId();
$node = $item->getNode();
$this->begin();
Expand All @@ -469,7 +473,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'catalog' );
}


Expand Down
53 changes: 53 additions & 0 deletions lib/mshoplib/src/MShop/Common/Manager/ListRef/Base.php
Expand Up @@ -212,4 +212,57 @@ protected function getRefItems( array $refIdMap )

return $items;
}


/**
* Adds new, updates existing and deletes removed referenced items
*
* @param \Aimeos\MShop\Common\Item\ListRef\Iface $item Item with referenced items
* @return \Aimeos\MShop\Common\Item\ListRef\Iface $item with updated referenced items
*/
protected function saveRefItems( \Aimeos\MShop\Common\Item\ListRef\Iface $item, $domain )
{
$rmListIds = $rmIds = [];
$context = $this->getContext();
$listManager = \Aimeos\MShop\Factory::createManager( $context, $domain . '/lists' );

foreach( $item->getDeletedItems() as $listItem )
{
$rmListIds[] = $listItem->getId();

if( ( $refItem = $listItem->getRefItem() ) !== null ) {
$rmIds[$listItem->getDomain()][] = $refItem->getId();
}
}

foreach( $rmIds as $refDomain => $ids ) {
\Aimeos\MShop\Factory::createManager( $context, $refDomain )->deleteItems( $ids );
}

$listManager->deleteItems( $rmListIds );

foreach( $item->getListItems( null, null, null, false ) as $listItem )
{
if( ( $refItem = $listItem->getRefItem() ) !== null )
{
if( $refItem instanceof \Aimeos\MShop\Common\Item\Domain\Iface ) {
$refItem->setDomain( $domain );
}

$manager = \Aimeos\MShop\Factory::createManager( $context, $listItem->getDomain() );
$refItem = $manager->saveItem( $refItem );

$listItem->setRefId( $refItem->getId() );
}

if( $listItem->getParentId() != $item->getId() ) {
$listItem->setId( null ); //create new list item if copied
}

$listItem->setParentId( $item->getId() );
$listManager->saveItem( $listItem );
}

return $item;
}
}
4 changes: 2 additions & 2 deletions lib/mshoplib/src/MShop/Customer/Manager/Standard.php
Expand Up @@ -376,7 +376,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
}

if( !$item->isModified() ) {
return $item;
return $this->saveRefItems( $item, 'customer' );
}

$context = $this->getContext();
Expand Down Expand Up @@ -561,7 +561,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )

$this->addGroups( $item );

return $item;
return $this->saveRefItems( $item, 'customer' );
}


Expand Down
4 changes: 2 additions & 2 deletions lib/mshoplib/src/MShop/Media/Manager/Standard.php
Expand Up @@ -283,7 +283,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
}

if( !$item->isModified() ) {
return $item;
return $this->saveRefItems( $item, 'media' );
}

$context = $this->getContext();
Expand Down Expand Up @@ -446,7 +446,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'media' );
}


Expand Down
4 changes: 2 additions & 2 deletions lib/mshoplib/src/MShop/Price/Manager/Standard.php
Expand Up @@ -316,7 +316,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
}

if( !$item->isModified() ) {
return $item;
return $this->saveRefItems( $item, 'price' );
}

$context = $this->getContext();
Expand Down Expand Up @@ -481,7 +481,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'price' );
}


Expand Down
4 changes: 2 additions & 2 deletions lib/mshoplib/src/MShop/Product/Manager/Standard.php
Expand Up @@ -215,7 +215,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
}

if( !$item->isModified() ) {
return $item;
return $this->saveRefItems( $item, 'product' );
}

$context = $this->getContext();
Expand Down Expand Up @@ -378,7 +378,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'product' );
}


Expand Down
4 changes: 2 additions & 2 deletions lib/mshoplib/src/MShop/Service/Manager/Standard.php
Expand Up @@ -308,7 +308,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
}

if( !$item->isModified() ) {
return $item;
return $this->saveRefItems( $item, 'service' );
}

$context = $this->getContext();
Expand Down Expand Up @@ -472,7 +472,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'service' );
}


Expand Down
4 changes: 2 additions & 2 deletions lib/mshoplib/src/MShop/Supplier/Manager/Standard.php
Expand Up @@ -260,7 +260,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
}

if( !$item->isModified() ) {
return $item;
return $this->saveRefItems( $item, 'supplier' );
}

$context = $this->getContext();
Expand Down Expand Up @@ -418,7 +418,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'supplier' );
}


Expand Down
4 changes: 2 additions & 2 deletions lib/mshoplib/src/MShop/Text/Manager/Standard.php
Expand Up @@ -169,7 +169,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
}

if( !$item->isModified() ) {
return $item;
return $this->saveRefItems( $item, 'text' );
}

$context = $this->getContext();
Expand Down Expand Up @@ -330,7 +330,7 @@ public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
throw $e;
}

return $item;
return $this->saveRefItems( $item, 'text' );
}


Expand Down
43 changes: 43 additions & 0 deletions lib/mshoplib/tests/MShop/Product/Manager/StandardTest.php
Expand Up @@ -170,6 +170,49 @@ public function testSaveUpdateDeleteItem()
}


public function testSaveItemRefItems()
{
$context = \TestHelperMShop::getContext();

$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context );
$typeManager = $manager->getSubManager( 'type' );

$item = $manager->createItem();
$item->setTypeId( $typeManager->findItem( 'default', [], 'product' )->getId() );
$item->setCode( 'unitreftest' );

$listManager = $manager->getSubManager( 'lists' );
$listTypeManager = $listManager->getSubManager( 'type' );

$listItem = $listManager->createItem();
$listItem->setTypeId( $listTypeManager->findItem( 'default', [], 'product' )->getId() );

$textManager = \Aimeos\MShop\Text\Manager\Factory::createManager( $context );
$textTypeManager = $textManager->getSubManager( 'type' );

$textItem = $textManager->createItem();
$textItem->setTypeId( $textTypeManager->findItem( 'name', [], 'product' )->getId() );


$item->addRefItem( 'text', $listItem, $textItem );

$item = $manager->saveItem( $item );
$item2 = $manager->getItem( $item->getId(), ['text'] );

$item->deleteRefItem( 'text', $listItem, $textItem );

$item = $manager->saveItem( $item );
$item3 = $manager->getItem( $item->getId(), ['text'] );

$manager->deleteItem( $item->getId() );


$this->assertEquals( 0, count( $item->getRefItems( 'text', 'name', 'default', false ) ) );
$this->assertEquals( 1, count( $item2->getRefItems( 'text', 'name', 'default', false ) ) );
$this->assertEquals( 0, count( $item3->getRefItems( 'text', 'name', 'default', false ) ) );
}


public function testSaveItemSitecheck()
{
$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperMShop::getContext() );
Expand Down

0 comments on commit 246d8b6

Please sign in to comment.