Page 2 of 2

Re: Admin rebuild : getting/creating/updating data

Posted: 19 Feb 2016, 09:49
by aimeos
Chaeril wrote: I keep getting error 'Class 'MShop_Factory' not found' is it changed in ~2016 or i missed something?
is it \Aimeos\MShop\Factory::createManager ?
Yes, it is. The code base has been rewritten to PSR-4 compliance (namespaces) and all former class names are now prefixed by "\Aimeos\" and underscores (_) have been replaced by backslashes (\). A full list of breaking changes is available in the developer section: https://aimeos.org/docs/Developers/Changelog

Re: Admin rebuild : getting/creating/updating data

Posted: 21 Feb 2016, 10:45
by Chaeril
1. if i want to find stock and / or media for product, do i need to set up another manager and submanager search?

Code: Select all

$manager = \Aimeos\MShop\Factory::createManager($context, 'product');
$search = $manager->createSearch();
$getSubManager = $manager->getSubManager('stock');
$sub_search = $getSubManager->createSearch();

...

$media = \Aimeos\MShop\Factory::createManager($context, 'media');
$s_media = $media->createSearch();

or there is another preferred way?.
2. When do i need to use getResourceType() and getSearchAttributes();
ResourceType seems to return an array which i can loop inside createManager () although it seems to be a bad way.
still not sure with SearchAttributes.

Regards

Re: Admin rebuild : getting/creating/updating data

Posted: 21 Feb 2016, 11:19
by aimeos
Media items (as well as attributes, texts, prices, etc.) are associated via the list tables. The easiest way to fetch them is along with the product item:

Code: Select all

$item = $manager->getItem( '<id>', array( 'media', ...) );
$items = $manager->searchItems( $search, array( 'media', 'text', 'attribute', 'price', ... ) );
Afterwards, they are available via getRefItems():

Code: Select all

$mediaItems = $item->getRefItems( 'media' [, '<media type>'] [, '<list type>'] );
More information about this methods is available in the API docs:
https://aimeos.org/api/2016/class-Aimeo ... Iface.html
https://aimeos.org/api/2016/class-Aimeo ... Iface.html


You can create the submanager for retrieving stock levels with the factory too:

Code: Select all

$manager = \Aimeos\MShop\Factory::createManager($context, 'product/stock');
getSearchAttributes() and getResourceType() are available to determine the search keys and resources automatically. Normally, you don't need to call them.

Re: Admin rebuild : getting/creating/updating data

Posted: 24 Feb 2016, 12:19
by aimeos
There's now full documentation available for how to manage items and content:
https://aimeos.org/docs/Developers/Libr ... ging_items

Re: Admin rebuild : getting/creating/updating data

Posted: 08 Mar 2016, 12:08
by Chaeril

Code: Select all

$json = null;
    $items = $manager->searchItems($search, array('media', 'text', 'attribute', 'price'));
    foreach ($items as $id => $item) {
      $item = $manager->getItem($id, array('media', 'text', 'attribute', 'price'));
      $temp = $item->toArray();
      $temp['text'] = $item->getRefItems('text');
      $temp['media'] = $item->getRefItems('media');
      $temp['attribute'] = $item->getRefItems('attribute');
      $temp['price'] = $item->getRefItems('price');
      $json[] = $temp;
    }
i get json response something like
[0] -> ... -> [media] -> [29] -> position -> 0
i found that 29 is the id of media attached to item, should i use another manager to find preview url, or file url.

Re: Admin rebuild : getting/creating/updating data

Posted: 08 Mar 2016, 12:38
by aimeos
No, but you should use the toArray() method for each item to get the key/value pairs suitable for json_encode:

Code: Select all

$json = array();
$items = $manager->searchItems($search, array('media', 'text', 'attribute', 'price'));

foreach ($item as $id => $item) {
    $temp = $item->toArray();
    foreach ($item->getRefItems() as $domain => $list) {
        foreach ($list as $refId => $refItem) {
            $temp[$domain][$refId] = $refItem->toArray();
        }
    }
    $json[] = $temp;
}
More information about getRefItems() and toArray():
https://aimeos.org/api/latest/class-Aim ... Iface.html
https://aimeos.org/api/latest/class-Aim ... l#_toArray

Re: Admin rebuild : getting/creating/updating data

Posted: 19 Mar 2016, 14:14
by Chaeril
Hi,
i am stuck with creating order, from what i understand i need to create 3 manager for each of order, order/base, and order/base/product. then save order/base, get last id and save the rest of data using .baseid
is there a transactional way to do this?

Re: Admin rebuild : getting/creating/updating data

Posted: 20 Mar 2016, 15:40
by aimeos
Chaeril wrote: i am stuck with creating order, from what i understand i need to create 3 manager for each of order, order/base, and order/base/product. then save order/base, get last id and save the rest of data using .baseid
is there a transactional way to do this?
You only need the order and order/base manager. The item created by an order/base manager is exactly the basket of a customer and you can save it by calling the store() method:

Code: Select all

$manager = \Aimeos\MShop\Factory::createManager( $context, 'order' );
$baseManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' );

$order = $manager->createItem();
$basket = $baseManager->createItem();
// ... add something to the basket ...

$manager->begin();
$baseManager->store( $basket );
$order->setBaseId( $basket->getId() );
$manager->saveItem( $order );
$manager->commit(); // or rollback()
For more information about the methods, please have a look into the API docs:
https://aimeos.org/api/latest/namespace ... nager.html