How to add additional fields to a product

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!
cyberbustard666
Posts: 1
Joined: 29 Aug 2021, 18:27

How to add additional fields to a product

Post by cyberbustard666 » 29 Aug 2021, 18:41

aimeos wrote: 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' );
}
Hello ,need to extend mshop_product table can i do it same as users , what in need extend instead of '\Aimeos\MShop\Common\Manager\Decorator\Base' becouse i don't found same methods in Product .Can i see some examples like your code but for my table or maybe documentation link ? I saw https://aimeos.org/docs/Developers/Libr ... ema_update but did no understand how use is for my table I use 2020.10.7 version

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

Re: How to add additional fields to a product

Post by aimeos » 31 Aug 2021, 05:58

It's the same as for customers, just replace every occurence of "customer" by "product" including the in the file names.

The latest documentation is available here:
https://aimeos.org/docs/latest/models/extend-managers/
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply