Add new field to users can't show and store the data

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
amJing
Posts: 2
Joined: 01 Sep 2022, 08:54

Add new field to users can't show and store the data

Post by amJing » 01 Sep 2022, 09:18

Hi There,
I followed this article https://aimeos.org/docs/latest/models/e ... /#easy-way to add new field to users,
First I created manager decorator at packages/cywextension/src/MShop/Customer/Manager/Decorator/CywCustomerDecorator.php

Code: Select all

<?php
namespace Aimeos\MShop\Customer\Manager\Decorator;

class CywCustomerDecorator extends \Aimeos\MShop\Common\Manager\Decorator\Base
implements \Aimeos\MShop\Common\Manager\Decorator\Iface
{
    private $attr = [
        'customer_id' => [
            'code' => 'customer.customer_id',
            'internalcode' => 'mcus."customer_id"',
            'label' => 'User customer id',
            'type' => 'integer',
            'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT,
        ],
    ];

    public function getSaveAttributes() : array
    {
        return parent::getSaveAttributes() + $this->createAttributes( $this->attr );
    }

    public function getSearchAttributes( bool $sub = true ) : array
    {
        return parent::getSearchAttributes( $sub ) + $this->createAttributes( $this->attr );
    }

}
then change the config in config/shop.php

Code: Select all

'mshop' => [
        'customer' => [
            'manager' => [
                'decorators' => [
                    'local' => ['CywCustomerDecorator']
                ]
            ],
        ]
],
then overwrite the template at packages/cywextension/templates/admin/jqadm/customer/item.php

Code: Select all

<div class="col-sm-8">
<?php if( $this->get( 'itemData/.modify' ) ) : ?>
<input class="form-control item-customerid" type="text" required="required" tabindex="1" autocomplete="off"
    name="<?= $enc->attr( $this->formparam( array( 'item', 'customer.customer_id' ) ) ) ?>"
    placeholder="<?= $enc->attr( $this->translate( 'admin', 'Customer ID (required)' ) ) ?>"
    value="<?= $enc->attr( $this->get( 'itemData/customer.customer_id' ) ) ?>"
    <?= $this->site()->readonly( $this->get( 'itemData/customer.siteid' ) ) ?> />
<?php else : ?>
<span class="form-control item-customerid">
    <?= $enc->html( $this->get( 'itemData/customer.customer_id' ) ) ?>
</span>
<?php endif ?>
</div>
BUT, the value for my new field "customer_id" can't show up on page, and when I input a value for "customer_id" and click save button, no error show up, then check DB table, the vlaue doesn't stored.

In order to see if I can get the data, I care decorator for jqadmin at packages/cywextension/src/Admin/JQAdm/Common/Decorator/CywCustomerDecorator.php, the following codes cause some errors :(

Code: Select all

namespace Aimeos\Admin\JQAdm\Common\Decorator;

class CywCustomerDecorator extends Base
{
    public function save() : ?string
    {
        return $this->getClient()->save();
    }

    public function get() : ?string
    {
        $view = $this->object()->data( $this->view() );
		try
		{
			if( ( $id = $view->param( 'id' ) ) === null )
			{
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
			}
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
			$view->item = $manager->get( $id, $this->getDomains() );
			// $view->itemData = $this->toArray( $view->item ); // this line has error Called unknown macro "toArray" on class "Aimeos\Admin\JQAdm\Customer\Standard"
			$view->itemBody = parent::get();

            print_r($view->item);
            // print_r($view->itemData);
		}
		catch( \Exception $e )
		{
			$this->report( $e, 'get' );
            dd($e);
            print_r("error=============");
		}
		return $this->render( $view ); // this line has error Called unknown macro "render" on class "Aimeos\Admin\JQAdm\Customer\Standard"
    }
}
then change the config in config/shop.php

Code: Select all

'admin' => [
        'jqadm' => [
            'customer' => [
                'decorators' => [
                    'global' => ['CywCustomerDecorator']
                ],
            ],
        ],
    ],
What I have done all listed here, could any one help me to figure out if I missing anything?
Thanks very much for any help!

amJing
Posts: 2
Joined: 01 Sep 2022, 08:54

Re: Add new field to users can't show and store the data

Post by amJing » 01 Sep 2022, 09:24

I think it's toArray and fromArray issue, so I did the follow things, but it still can't work.
First to add decorator for customer item at packages/cywextension/src/MShop/Customer/Item/Decorator/CywCustomerItemDecorator.php

Code: Select all

namespace Aimeos\MShop\Customer\Item;

class CywCustomerItemDecorator extends Standard
{
    private $myvalues;

    public function __construct(
        \Aimeos\MShop\Common\Item\Address\Iface $address,
        array $values = [],
        array $listItems = [],
        array $refItems = [],
        array $addrItems = [],
        array $propItems = []
    ) {
        parent::__construct($address, $values, $listItems, $refItems, $addrItems, $propItems);
        print_r($values);exit;
        $this->myvalues = $values;
    }

    public function getCustomerId(): int
    {
        if (isset($this->myvalues['customer_id'])) {
            return (int) $this->myvalues['customer_id'];
        }
        return 0;
    }

    public function setCustomerId(?int $val): \Aimeos\MShop\Common\Item\Iface
    {
        if ((int) $val !== $this->getLocationId()) {
            $this->values['customer_id'] = (int) $val;
            $this->setModified();
        }
        return $this;
    }

    public function fromArray(array &$list, bool $private = false): \Aimeos\MShop\Common\Item\Iface
    {
        $unknown = [];
        $item = parent::fromArray($list, $private);

        foreach ($list as $key => $value) {
            switch ($key) {
                case 'customer_id':
                    $item = $item->setCustomerId($value);
                    break;
                default:
                    continue 2;
            }
            unset($list[$key]);
        }

        return $item;
    }

    public function toArray(bool $private = false): array
    {
        $list = parent::toArray($private);
        print_r('Here is itemmmmmmmmmm+++++++++++++++++');exit; // can't print the value

        if ($private === true) {
            $list['customer_id'] = $this->getCustomerId();
        }

        return $list;
    }
}
then change config in config/shop.php

Code: Select all

'mshop' => [
    'customer' => [
        'item' => [
            'decorators' => [
                'local' => ['CywCustomerItemDecorator']
            ]
        ],
        'manager' => [
            'decorators' => [
                'local' => ['CywCustomerDecorator']
            ]
        ],
    ]
],
I did research everywhere, but don't know how to move on, please helpppp!! Thanks!!

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

Re: Add new field to users can't show and store the data

Post by aimeos » 02 Sep 2022, 10:08

The code must be "customer_id" only, not "customer.customer_id" everywhere because the column is named customer_id and that's exactly what's returned by the database.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply