Linking Custom Domain Manager to Media Domain in JSON:API

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!
User avatar
Jairus34
Posts: 30
Joined: 05 Aug 2024, 07:34

Linking Custom Domain Manager to Media Domain in JSON:API

Post by Jairus34 » 09 Dec 2024, 02:56

Hi,

I’ve set up a new domain manager along with a list-type manager to store data with images. Now, I’m wondering if there’s a way to link it to the media domain. I’m using JSON:API.

Could you guide me on how to go about this?

Thanks!

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

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by aimeos » 09 Dec 2024, 10:34

If everything is correctly implemented, you can get the media items attached to your new domain items using "include=media"
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
Jairus34
Posts: 30
Joined: 05 Aug 2024, 07:34

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by Jairus34 » 09 Dec 2024, 11:04

I haven’t created the GET method for JSON:API yet. I’m still figuring out how to attach media items and store them in storage using the POST method in JSON:API.

So far, I’ve created:
1. mshop_trade (main domain)
2. mshop_trade_list
3. mshop_trade_list_type

I’ve also set up the managers for mshop_trade and am currently working on the POST method for JSON:API."

Is there a guide or resource that can help me achieve this? Thank you!

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

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by aimeos » 11 Dec 2024, 10:44

The customer JSON:API endpoint has implemented handling POST requests:
https://github.com/aimeos/ai-client-jso ... #L266-L300

To store uploaded files, you should use the upload() method of the media manager:
https://github.com/aimeos/aimeos-core/b ... /Iface.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
Jairus34
Posts: 30
Joined: 05 Aug 2024, 07:34

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by Jairus34 » 01 Jan 2025, 03:42

Hi, I created a lists table called Mshop_trade_list to manage relationships for my main domain Mshop_trade. Should I follow the documentation and create a class for Mshop_trade_list as mentioned here: https://aimeos.org/docs/latest/models/c ... ing-domain?

Also, I’m having trouble with the createListItem and getRefItems method. Any ideas on what might be causing the issue?

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

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by aimeos » 01 Jan 2025, 10:02

Yes, you need an Iface and a Standard class for a Lists and Type manager. Use the product domain as example:
https://github.com/aimeos/aimeos-core/t ... ager/Lists
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
Jairus34
Posts: 30
Joined: 05 Aug 2024, 07:34

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by Jairus34 » 01 Jan 2025, 10:07

Thank you for your response.

I noticed that the product lists and type manager do not have a getSaveAttributes() function. Just to confirm, does this mean I need to create one similar to the example in the documentation?

Code: Select all

namespace Aimeos\MShop\Product\Manager\Test;

class Standard
    extends \Aimeos\MShop\Common\Manager\Base
    implements \Aimeos\MShop\Common\Manager\Iface
{
    public function getSaveAttributes() : array
    {
        return $this->createAttributes( [
            'parentid' => [
                'type' => 'int',
                'public' => false
            ],
            'label' => [
            ],
            'status' => [
                'type' => 'int',
            ],
            'position' => [
                'type' => 'int',
                'label' => 'Position for sorting'
            ],
        ] );
    }
}

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

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by aimeos » 01 Jan 2025, 10:14

No. Use the product list and list type manager as example. Creating list and type managers is simpler than custom domain managers but not yet documented separately.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
Jairus34
Posts: 30
Joined: 05 Aug 2024, 07:34

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by Jairus34 » 04 Jan 2025, 10:03

Hi, I have followed the steps and examples, but when I try to use createListItem(), there is an error.

Code: Select all

Called unknown method "createListItem" on class "Aimeos\Mshop\Trade\Manager\Standard"
//POST Jsonapi

Code: Select all

public function post(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $view = $this->view();
        $context = $this->context();

        try {
            $body = (string) $request->getBody();

            if (($payload = json_decode($body)) === null || !isset($payload->data->attributes)) {
                throw new \Aimeos\Client\JsonApi\Exception('Invalid JSON in body', 400);
            }

            $manager = \Aimeos\MShop::create($context, 'trade');
            $manager->begin();

            $attributes = (array) $payload->data->attributes;

            $item = $manager->create()->fromArray($attributes);

            if (isset($attributes['trade.services']) && is_array($attributes['trade.services'])) {

                foreach ($attributes['trade.services'] as $key => $value) {
                    $listitem = $manager->createListItem();
                    $listitem->setRefId($value)
                        ->setPosition($key);

                    $item->addListItem('services', $listitem);
                }
            }

            $workshop = $manager->save($item);
            $status = 201;

            $manager->commit();

            $view->item = $workshop;
        } catch (\Aimeos\MShop\Exception $e) {
            $manager->rollback();
            $status = 404;
            $view->errors = $this->getErrorDetails($e, 'mshop');
        } catch (\Exception $e) {
            $manager->rollback();
            $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
            $view->errors = $this->getErrorDetails($e);
        }

        return $response->withHeader('Allow', 'PATCH,POST')
            ->withHeader('Cache-Control', 'no-cache, private')
            ->withHeader('Content-Type', 'application/vnd.api+json')
            ->withBody($view->response()->createStreamFromString($body))
            ->withStatus($status);
    }
//Standard Manager

Code: Select all

class Standard
    extends \Aimeos\MShop\Common\Manager\Base
    implements \Aimeos\MShop\Common\Manager\Iface
{
    public function getSaveAttributes(): array
    {
        return $this->createAttributes([
            'domain' => [
                'code' => 'trade.domain',
                'internalcode' => 'mtra."domain"',
                'label' => 'Domain',
                'type' => 'string'
            ],
            'full_name' => [
                'code' => 'trade.full_name',
                'internalcode' => 'mtra."full_name"',
                'label' => 'Full Name',
                'type' => 'string'
            ],
            'email' => [
                'code' => 'trade.email',
                'internalcode' => 'mtra."email"',
                'label' => 'Email',
                'type' => 'string'
            ],
            'phone' => [
                'code' => 'trade.phone',
                'internalcode' => 'mtra."phone"',
                'label' => 'Phone',
                'type' => 'string'
            ],
            'info' => [
                'code' => 'trade.info',
                'internalcode' => 'mtra."info"',
                'label' => 'Additional Information',
                'type' => 'string'
            ],
            'booking_date' => [
                'code' => 'trade.booking_date',
                'internalcode' => 'mtra."booking_date"',
                'label' => 'Booking Date',
                'type' => 'datetime',
            ],
            'agree' => [
                'code' => 'trade.agree',
                'internalcode' => 'mtra."agree"',
                'label' => 'Agree',
                'type' => 'string'
            ],
            'status' => [
                'code' => 'trade.status',
                'internalcode' => 'mtra."status"',
                'label' => 'Status',
                'type' => 'string'
            ]
        ]);
    }

    public function create (array $value = []) : \Aimeos\MShop\Trade\Item\Iface
    {
        $value['trade.siteid'] = $value['trade.siteid'] ?? $this->context()->locale()->getSiteId();
        return $this->createItemBase( $value );
    }

    protected function createItemBase( array $values = [] ) : \Aimeos\MShop\Trade\Item\Iface
    {
        return new \Aimeos\MShop\Trade\Item\Standard( $values );
    }
}

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

Re: Linking Custom Domain Manager to Media Domain in JSON:API

Post by aimeos » 06 Jan 2025, 15:48

Add the Lists decorator to your domain: https://github.com/aimeos/aimeos-core/b ... ct.php#L13
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply