How to load media associated with an attribute?

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
swpierce
Posts: 53
Joined: 01 Nov 2015, 13:19

How to load media associated with an attribute?

Post by swpierce » 15 Dec 2015, 18:50

I have an attribute with code 'attr-code1'
I have a media entry defined and associated with that attribute. Everything in the admin panel looks fine. If I open that attribute and click on the media tab, I see the media. In the database, the appropriate stuff is in mshop_attribute_list, etc.

I try to load the referenced media in my code like this (might not be most efficient?):

Code: Select all

                        $attributeManager = MShop_Factory::createManager( $context, 'attribute' );
                        $search = $attributeManager->createSearch( true );
                        $expr = array(
                            $search->getConditions(),
                            $search->compare( '==', 'attribute.code', 'attr-code1' ),
                        );
                        $search->setConditions( $search->combine( '&&', $expr ) );
                        $search->setSortations( array( $search->sort( '-', 'attribute.id' ) ) );
                        
                        $dlAttrList = $attributeManager->searchItems( $search );
                        $downloadAttribute = reset( $dlAttrList );

                        foreach( $downloadAttribute->getListItems( 'media', 'default' ) as $media )
                        {
                            // do stuff
                        }
The attribute loads just fine. The call get getListItems( 'media', 'default' ) returns an empty list. I also tried getListItems( 'media' ) and getListItems() - all return an empty list.

What am I doing wrong?

swpierce
Posts: 53
Joined: 01 Nov 2015, 13:19

Re: How to load media associated with an attribute?

Post by swpierce » 15 Dec 2015, 21:42

Found it. Forgot to add the media references to the searchItems so the references would get loaded. Need more coffee. :shock:

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

Re: How to load media associated with an attribute?

Post by aimeos » 16 Dec 2015, 09:56

swpierce wrote:

Code: Select all

                        $dlAttrList = $attributeManager->searchItems( $search );
You have to tell the manager that it should load the referenced attributes to:

Code: Select all

$attributeManager->searchItems( $search, array( 'attribute' ) );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply