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)
#3079 by jossnaz
26 Jul 2016, 01:06
i just saw that properties are not of the same domain like products. Now what is the efficient and easy way to retrieve the properties? see here: https://aimeos.org/docs/Developers/Libr ... ta_domains

I ended up doing something like this, which seems stupid:

Code: Select all 
  /**
   * @param $context
   * @param $catIdsArr
   * @param bool|true $retrievePropItems
   * @return array[\Aimeos\MShop\Common\Item\Iface[], \Aimeos\MShop\Product\Item\Property\Standard[]]
   * @throws \Aimeos\MShop\Exception
   */
  public static function GetProductsInExactlyTheseCats($context, $catIdsArr, $retrievePropItems = true){
    $manager = \Aimeos\MShop\Index\Manager\Factory::createManager($context );
    $search = $manager->createSearch(true);
    $expr[] = $search->getConditions();
    $fcn = $search->createFunction( 'index.catalogcount', array( 'default', $catIdsArr ) );
    $search->setConditions( $search->compare( '==', $fcn, sizeof($catIdsArr) ) );
  /** @var \Aimeos\MShop\Common\Item\Iface[] $myitems */
    $myitems = $manager->searchItems($search, array('text'));


    /** @var \Aimeos\MShop\Product\Item\Property\Standard[] $myPropItems */
    if($retrievePropItems && sizeof($myitems) > 0){

      foreach($myitems as $key => $item){
        $propManager = \Aimeos\MShop\Factory::createManager( $context, 'product/property' );
        $search = $propManager->createSearch(true);
        $expr = $search->compare( '==', 'product.property.parentid', array($item->getId()) );
        $search->setConditions( $expr );
        $myPropItems = $propManager->searchItems($search);
        if(sizeof($myPropItems) > 0){
          $myitems[$key] = array($item, $myPropItems);
        }
      }
    }


    return $myitems;
  }
#3083 by aimeos
26 Jul 2016, 20:54
It's very inefficient because you fetch the properties for each product separately. Better use this:

Code: Select all// ...
$myitems = $manager->searchItems($search, array('text'));

$propManager = \Aimeos\MShop\Index\Manager\Factory::createManager($context, 'product/property');

$search = $propManager->createSearch(true);
$expr = array(
    $search->compare('==', 'product.property.parentid', array_keys($myitems)),
    $search->getConditions(),
);
$search->setConditions($search->combine('&&', $expr);
$search->setSlice(0, 0x7fffffff);

$propItems = $propManager->searchItems($search);
$result = array();

foreach($propItems as $item) {
    $result[$item->getParentId()][$item->getId()] = $item;
}


Then you have the properties grouped by the product ID and can access them in a very efficient way while using only one additional query.