Help for integrating the Laravel package
Forum rules: Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
#1648 by Yvler
15 Jun 2015, 14:37
Hi

As I told in another post, we're trying to rewrite the admin panel.
Is there an easy way (à la Laravel way) to receive a list (for example of products, categories, ...) of items?
Is there another way to save and update them, or create them? :) Or do we just do the standard Laravel stuff and write to the database?
#1649 by aimeos
15 Jun 2015, 16:16
Yvler wrote:As I told in another post, we're trying to rewrite the admin panel.


Offering an admin interface suited for the specific needs is always a good thing. Our plan is still to offer a REST interface to ease that task.

Yvler wrote:Is there an easy way (à la Laravel way) to receive a list (for example of products, categories, ...) of items?
Is there another way to save and update them, or create them? :) Or do we just do the standard Laravel stuff and write to the database?


The principles are more or less similar but Aimeos provides a much better suited interface tailored for e-commerce applications to the data than just accessing the records. Unfortunately, tutorials for this are still missing but the methods are fully documented in the source code.

In Aimeos managers are responsible for creating, saving, deleting and searching items. The items are more or less containers for the records with a bit intelligence while the managers know how to create and handle them. Each domain (e.g. catalog, product, text, attribute, etc.) has its own manager (maybe with sub-managers) and items. As a rule of thumb, each table in the database is governed by a manager or sub-manager.

All managers share a common interface:
- createItem()
- saveItem()
- deleteItem()
- searchitems()

searchItems() is the most powerful method and I have to explain it a bit more. You can add condition and sorting rules for each search as well as the domains associated items should be fetched too. For example:

Code: Select all$context = app( '\Aimeos\Shop\Base\Context' )->get(); // necessary context with required depencencies
$manager = MShop_Factory::createManager( $context, 'product' ); // 'product/list' would return the product list sub-manager

$search = $manager->createSearch(true);
$expr = array(
  $search->compare( '=~', 'product.code', 'demo-' );
  $search->compare( '==', 'product.list.domain', 'text' );
  $search->compare( '>=', 'product.list.datestart', '2000-01-01 00:00:00' );
  $search->getConditions(),
);
$search->setConditions( $search->combine( '&&', $expr ); // AND operator, || -> OR operator
$search->setSortations( array( $search->sort( '+', 'product.id' ); // + -> ascending, - -> descending
$search->setSlice( 10, 50 ); // start at position 10, 50 entries

$total = 0;
$result = $manager->searchItems( $search, array( 'text' ), $total ); // last two parameters are optional


This would fetch the product items whose code starts with "demo-" and which have at least one text item associated that is available after year 2000. The are ordered by product ID and the items at position 10 to 60 are returned which the associated text items available via "$item->getRefItem( 'text', ... );". The $total parameter contains the number of total items that are available for that search.

The list of conditions is described in the Wiki: http://aimeos.org/docs/Developers/Conditions_for_searching
How to fetch bunches of items efficiently is also described there: http://aimeos.org/docs/Developers/Fetch_records_in_bunches
The available search keys for each manager are described in the managers, e.g. for the product manager: https://github.com/aimeos/arcavias-core/blob/master/lib/mshoplib/src/MShop/Product/Manager/Default.php

For the categories, the same principles apply but the catalog is a tree so it contains an addition method getTree() that returns a tree of nodes independent of how the tree is stored in the database.

What else do you need to know?
#1654 by Yvler
16 Jun 2015, 09:29
We're trying to use that code in our Controller class in app/Http/Controllers/ProductController.php
But we're not getting it working :/

Which namespaces/implements do we have to do?
We're used to the Laravel code and we're not seeing the wood for the trees...

Do we need to make an extension? And if so, is it possible to provide us a simple example from which we can start?
This is the code we've had so far:

Code: Select all<?php namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Barryvdh\Debugbar\Controllers\BaseController;
use Illuminate\Auth\Guard;
use App\Models;

use Illuminate\Http\Request;
use MyProject\Proxies\__CG__\OtherProject\Proxies\__CG__\stdClass;

class CategoryController extends Controller implements \MShop_Product_Manager_Interface{

    private $_manager = null;
    private $_context = null;
    /**

     */
    public function __construct()
    {
        $context = app( '\Aimeos\Shop\Base\Context' )->get();


        $this->_manager = \MShop_Catalog_Manager_Factory::createManager( $context );
        $this->_context = $context;
    }



Sorry for all these questions by the way..
#1655 by aimeos
16 Jun 2015, 09:48
Actually, it's very simple. Implement a basic Laravel controller (http://laravel.com/docs/5.1/controllers#basic-controllers) and in the actions, get the Aimeos context, create the manager you need and hand over the data you've received from the manager (or save/delete items using the managers):

Code: Select allnamespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    public function getTree()
    {
        $context = app( '\Aimeos\Shop\Base\Context' )->get();
        $manager = \MShop_Factory::createManager( $context, 'catalog' );

        // 'mypackage.mytemplate' must be adapted to your application
        return view('mypackage.mytemplate', ['tree' => $manager->getTree()]);
    }
}
#1675 by Yvler
23 Jun 2015, 12:19
Soooo...
I've added a product with the product manager
used the stock submanager to add the stock
created a new manager to add a price
added the price with a list submanager to the product.. also aded the description and meta data with this submanager.
and finally added the product to the catalog with a catalog/list manager

Everything seems to be added to the database, but I can't see the product on the homepage or under the categories..
Is there a cache I need to clear?
I already tried "\MShop_Factory::clear();" but that doesn't seem to work.

I do see this product when I surf to it directly (with the ID)


PS. There's a bug in the rebate percentage calculation. I added a price of 100 with a rebate of 10 and the rebate percentage displaying is 9%
#1676 by aimeos
23 Jun 2015, 12:35
Yvler wrote:Everything seems to be added to the database, but I can't see the product on the homepage or under the categories..
Is there a cache I need to clear?
I already tried "\MShop_Factory::clear();" but that doesn't seem to work.
I do see this product when I surf to it directly (with the ID)


Almost done. There are two further things you need to do:
- Create a catalog index manager and pass the product to the rebuildIndex() method to update the catalog index (example: https://github.com/aimeos/arcavias-core/blob/master/controller/extjs/src/Controller/ExtJS/Catalog/List/Default.php#L62)
- Clear the content cache for the category you've added the product to (example: https://github.com/aimeos/arcavias-core/blob/master/controller/extjs/src/Controller/ExtJS/Abstract.php#L263)
#1677 by aimeos
23 Jun 2015, 12:40
Yvler wrote:There's a bug in the rebate percentage calculation. I added a price of 100 with a rebate of 10 and the rebate percentage displaying is 9%


This is a common mistake in thinking. The price is the current value, the rebate is the amount the original price was reduced by, so
- current: 100
- rebate: 10
- original: 110
-> 10 * 100 / 110 = 9%
Thus, the original price was reduced by 9% compared to the current price :-)