How to add additional fields to a customer

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
aimeos
Administrator
Posts: 7858
Joined: 01 Jan 1970, 00:00

Re: How to add additional fields to a customer

Post by aimeos » 23 Jul 2019, 09:41

If your constructor is called (check with error_log()) then your createItemBase() would be used too because of:
https://github.com/aimeos/aimeos-core/b ... ts.php#L83

Exception: Your SQL doesn't return any items!

In the ./config/shop.php use the same as in your config file:

Code: Select all

'mshop' => array(
    'customer' => array(
        'manager' => array(
            'name' => 'Customer',
        ),
    ),
),
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to add additional fields to a customer

Post by MikaelNazarenko » 23 Jul 2019, 09:57

I moved necessary configuration to /var/www/labor/config/shop.php:

Code: Select all

'mshop' => [
        'customer' => array(
            'manager' => [
                'name' => 'Customer',
                'standard' => [
                    'delete' => array(
                        'ansi' => '
					DELETE FROM "users"
					WHERE :cond
				',
                    ),
                    'insert' => array(
                        'ansi' => '
					INSERT INTO "users" (
						"siteid", "name", "email", "company", "vatid", "salutation", "title",
						"firstname", "lastname", "address1", "address2", "address3",
						"postal", "city", "state", "countryid", "langid", "telephone",
						"telefax", "website", "longitude", "latitude",
						"birthday", "status", "vdate", "password",
						"updated_at", "editor", "created_at"
					) VALUES (
						?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
					)
				',
                    ),
                    'update' => array(
                        'ansi' => '
					UPDATE "users"
					SET "siteid" = ?, "name" = ?, "email" = ?, "company" = ?, "vatid" = ?,
						"salutation" = ?, "title" = ?, "firstname" = ?, "lastname" = ?,
						"address1" = ?, "address2" = ?, "address3" = ?, "postal" = ?,
						"city" = ?, "state" = ?, "countryid" = ?, "langid" = ?,
						"telephone" = ?, "telefax" = ?, "website" = ?,
						"longitude" = ?, "latitude" = ?, "birthday" = ?,
						"status" = ?, "vdate" = ?, "password" = ?, "updated_at" = ?, "editor" = ?
					WHERE "id" = ?
				',
                    ),
                    'search' => array(
                        'ansi' => '
					SELECT lvu."id" AS "customer.id", lvu."siteid" AS "customer.siteid",
						lvu."name" AS "customer.label", lvu."email" AS "customer.code",
						lvu."company" AS "customer.company", lvu."vatid" AS "customer.vatid",
						lvu."salutation" AS "customer.salutation", lvu."title" AS "customer.title",
						lvu."firstname" AS "customer.firstname", lvu."lastname" AS "customer.lastname",
						lvu."address1" AS "customer.address1", lvu."address2" AS "customer.address2",
						lvu."address3" AS "customer.address3", lvu."postal" AS "customer.postal",
						lvu."city" AS "customer.city", lvu."state" AS "customer.state",
						lvu."countryid" AS "customer.countryid", lvu."langid" AS "customer.languageid",
						lvu."telephone" AS "customer.telephone",lvu."telefax" AS "customer.telefax",
						lvu."email" AS "customer.email", lvu."website" AS "customer.website",
						lvu."longitude" AS "customer.longitude", lvu."latitude" AS "customer.latitude",
						lvu."birthday" AS "customer.birthday", lvu."status" AS "customer.status",
						lvu."vdate" AS "customer.dateverified", lvu."password" AS "customer.password",
						lvu."created_at" AS "customer.ctime", lvu."updated_at" AS "customer.mtime",
						lvu."editor" AS "customer.editor"
					FROM "users" AS lvu
					:joins
					WHERE :cond
					GROUP BY lvu."id", lvu."siteid", lvu."name", lvu."company", lvu."vatid",
						lvu."salutation", lvu."title", lvu."firstname", lvu."lastname",
						lvu."address1", lvu."address2", lvu."address3", lvu."postal",
						lvu."city", lvu."state", lvu."countryid", lvu."langid",
						lvu."telephone", lvu."telefax", lvu."email", lvu."website",
						lvu."longitude", lvu."latitude", lvu."birthday", lvu."status",
						lvu."vdate", lvu."password", lvu."created_at", lvu."updated_at",
						lvu."editor"
					/*-orderby*/ ORDER BY :order /*orderby-*/
					LIMIT :size OFFSET :start
				',
                    ),
                    'count' => array(
                        'ansi' => '
					SELECT COUNT(*) AS "count"
					FROM (
						SELECT DISTINCT lvu."id"
						FROM "users" AS lvu
						:joins
						WHERE :cond
						LIMIT 10000 OFFSET 0
					) AS list
				',
                    ),
                    'newid' => array(
                        'db2' => 'SELECT IDENTITY_VAL_LOCAL()',
                        'mysql' => 'SELECT LAST_INSERT_ID()',
                        'oracle' => 'SELECT users.CURRVAL FROM DUAL',
                        'pgsql' => 'SELECT lastval()',
                        'sqlite' => 'SELECT last_insert_rowid()',
                        'sqlsrv' => 'SELECT SCOPE_IDENTITY()',
                        'sqlanywhere' => 'SELECT @@IDENTITY',
                    ),
                ]
            ]
        ),
    ],
And I deleted /var/www/labor/ext/labor/mshop.php

Manager constructor called 200%:

Code: Select all

    public function __construct(\Aimeos\MShop\Context\Item\Iface $context)
    {
        error_log("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");

        parent::__construct($context);
    }
I got error in file log.

But createItemBase is not called!

When I try access admin, any page, for example http://labor/admin/default/jqadm/search ... er?lang=en,
I get '403 This action is unauthorized.' (((((((((((((((((((((((

I noticed even if I modify search query with error - I don't get any errors!

And what does mean 'ansi' ? Encoding ? Maybe I have to use another ?

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

Re: How to add additional fields to a customer

Post by aimeos » 23 Jul 2019, 10:06

Don't think it's a bug in Aimeos because others have extended the customer manager in the past too without problems.

My suggestion: Start from scratch because you now have made a lot of changes that may cause problems.
First, copy the Laravel customer manager to a new file, rename the class, extend from the Laravel manager and configure the new name of the class. Then you can check if everthing works, remove those parts of the class you don't need and finally copy the item class to a new name, overwriting createItemBase() and see what's happening.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to add additional fields to a customer

Post by MikaelNazarenko » 23 Jul 2019, 20:55

I am a champion!! Problem is solved! )) I had to extend my manager from Aimeos\MShop\Customer\Manager\Laravel and not from Aimeos\MShop\Customer\Manager\Standard

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

Re: How to add additional fields to a customer

Post by aimeos » 23 Jul 2019, 21:14

We've done a first step today to make it easier to manage additional columns in tables. Custom columns are now retrieved automatically by the SQL "search" statement and are available as "$item->columnname" in the item, e.g.

Code: Select all

$value = $item->mycolumn;
$item->mycolumn = 'newvalue';
Not perfect yet because the fromArray() and toArray() methods don't know about the new properties but it's a step forward :-)
If you want to test, the new code is in aimeos/aimeos-core:2019.07.x-dev and aimeos/ai-laravel:2019.07.x-dev
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to add additional fields to a customer

Post by MikaelNazarenko » 23 Jul 2019, 21:26

Thank you a lot for help!!! I will test later. Now it is ok current version for me) I've spent pretty much time, but I have learned Aimeos more and now I am very satisfied ! ) :twisted:

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

Re: How to add additional fields to a customer

Post by aimeos » 21 Aug 2019, 13:43

Starting with the upcoming 2019.10 release, managing custom columns will be super easy.

Extend the table (like already available) in ./ext/<your_extension>/lib/custom/setup/default/schema/customer.php:

Code: Select all

return array(
	'table' => array(
		'users' => function ( \Doctrine\DBAL\Schema\Schema $schema ) {
			$table = $schema->getTable( 'users' );
			$table->addColumn( 'mycolumn', 'string', array( 'length' => 64 ) );
			return $schema;
		},
);
Extend the customer manager by the 'mycolumn' column in ./ext/<your_extension>/lib/custom/src/MShop/Customer/Manager/Decorator/Myproject.php:

Code: Select all

namespace Aimeos\MShop\Customer\Manager\Decorator;

class Myproject
	extends \Aimeos\MShop\Common\Manager\Decorator\Base
{
	private $attr = [
		'mycolumn' => [
			'code' => 'mycolumn',
			'internalcode' => 'mcus."mycolumn"',
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
		]
	];

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

	public function getSearchAttributes( $withsub = true )
	{
		return parent::getSearchAttributes( $withsub ) + $this->createAttributes( $this->attr );
	}
}
Configure the new decorator in configuration (mshop section):

Code: Select all

	'mshop' => [
		'customer' => [
			'manager' => [
				'decorator' => [
					'local' => ['Myproject']
				]
			]
		]
	]
Aimeos cares about storing and retrieving the data automatically without any further code. Now you can do:

Code: Select all

$manager = \Aimeos\MShop::create( $context, 'customer' );
$item = $manager->createItem()->set( 'mycolumn', 'myvalue' );
$item = $manager->saveItem( $item );

$search = $manager->createSearch();
$search->setConditions( $search->compare( '==', 'mycolumn', 'myvalue' ) );
foreach( $manager->searchItems( $search ) as $item ) {
	echo $item->get( 'mycolumn', 'default value' );
}
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply