New media items not being added if ->deleteListItems() is used beforehand.

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kdim95
Advanced
Posts: 194
Joined: 26 Aug 2022, 12:17

New media items not being added if ->deleteListItems() is used beforehand.

Post by kdim95 » 22 May 2023, 13:43

Laravel framework version: 9.52.4
Aimeos Laravel version: ~2022.10
PHP Version: 8.2.4
Environment: Linux

Hello,

Why are the new list items not added if I use deleteListItems() on the old list items beforehand?

The goal is to clear old media list items of a specific type and then add them anew.

Code: Select all

$media_list_items = $product_item->getListItems( 'media', 'default', 'page', false );

$product_item->deleteListItems( $media_list_items, true ); // New media list items are not added if this method is used
				
if( isset( $book_info['pages'] ) && sizeof( $book_info['pages'] ) > 0 ) {
	foreach( $book_info['pages'] as $page ) {
		$parts = explode( "/", $page );
		$label = end( $parts );

		$media_item = $media_manager->create();
		$media_item->setType('page');
		$media_item->setDomain( 'product' );
		$media_item->setUrl( $page );
		$media_item->setPreview( $page );
		$media_item->setMimeType( 'image/jpeg' );
		$media_item->setLabel( $label );
							
		$media_list_item = $product_manager->createListItem();
		$product_item->addListItem( 'media', $media_list_item, $media_item );
	}
}

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: New media items not being added if ->deleteListItems() is used beforehand.

Post by aimeos » 24 May 2023, 09:10

Are you sure that addListItem() is actually called?

If you remove all items first and add them again afterwards, it will be pretty slow due to the amount of changes in the database. In the Aimeos code base, list and domain items are reused as often as possible to minimize database changes and to speed up imports.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 194
Joined: 26 Aug 2022, 12:17

Re: New media items not being added if ->deleteListItems() is used beforehand.

Post by kdim95 » 25 May 2023, 10:57

I'm pretty sure that addListItem() is called, because if I remove deleteListItems(), the items are added.

I think it's some kind of conflict, because items are marked for deletion before they are even added.

Removing all items and re-adding them with the updated ones (if there are changes) simplifies the process and the amount of code for me.

Otherwise I will have to go through every individual file and compare the file names or file sizes, etc.

Also I won't be using this all the time, so it shouldn't take too much resources.

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: New media items not being added if ->deleteListItems() is used beforehand.

Post by aimeos » 26 May 2023, 09:59

Removing referenced items is done here:
https://github.com/aimeos/aimeos-core/b ... p#L97-L139

They are then returned here:
https://github.com/aimeos/aimeos-core/b ... #L153-L166

And finally deleted here:
https://github.com/aimeos/aimeos-core/b ... #L254-L274

Maybe your can find out now why you have that behavior.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply