Page 1 of 1

How to load media associated with an attribute?

Posted: 15 Dec 2015, 18:50
by swpierce
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?

Re: How to load media associated with an attribute?

Posted: 15 Dec 2015, 21:42
by swpierce
Found it. Forgot to add the media references to the searchItems so the references would get loaded. Need more coffee. :shock:

Re: How to load media associated with an attribute?

Posted: 16 Dec 2015, 09:56
by aimeos
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' ) );