Add an attribute with a decorator, but private?
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!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Add an attribute with a decorator, but private?
Hello,
I'm attempting to add another attribute to the Review item, but have it private.
The attribute is supposed to store the user IP of the reviewer.
The problem is that the IP attribute is visible in the json response when searching for reviews with the jsonapi.
I attempted to add the " 'public' => false " key thinking that it would solve this problem, but it did not solve it.
What is the proper way to create custom attributes which are private?
I'm attempting to add another attribute to the Review item, but have it private.
The attribute is supposed to store the user IP of the reviewer.
The problem is that the IP attribute is visible in the json response when searching for reviews with the jsonapi.
I attempted to add the " 'public' => false " key thinking that it would solve this problem, but it did not solve it.
Apparently this is being handled in the Review Item itself in the toArray() method.private $attr = [
'ip' => [
'code' => 'ip',
'internalcode' => 'mrev."ip"',
'label' => 'Customer IP address',
'type' => 'string',
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
'public' => false,
]
];
What is the proper way to create custom attributes which are private?
Re: Add an attribute with a decorator, but private?
At the moment, the only way is to overwrite the toArray() method in your own review item:
https://github.com/aimeos/aimeos-core/b ... #L304-L324
How to overwrite items and managers:
https://aimeos.org/docs/latest/models/e ... custom-way
https://github.com/aimeos/aimeos-core/b ... #L304-L324
How to overwrite items and managers:
https://aimeos.org/docs/latest/models/e ... custom-way
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
Re: Add an attribute with a decorator, but private?
I did it a bit differently before you responded. Is this solution safe to work with?
I created a Review Manager Decorator, a custom Review Manager and a custom Review Item.
In the custom Review Item, I use unset() to remove the ip if its private.
This way I don't have to override the database manager insert, update, search configs.
Review Manager Decorator ( add IP attribute ):
Custom Review Item ( implement getIp(), setIp(), fromArray() and toArray() methods ):
Custom Review Manager ( implement createItemBase() to return custom Review Item ):
I created a Review Manager Decorator, a custom Review Manager and a custom Review Item.
In the custom Review Item, I use unset() to remove the ip if its private.
This way I don't have to override the database manager insert, update, search configs.
Review Manager Decorator ( add IP attribute ):
Code: Select all
<?php
namespace Aimeos\MShop\Review\Manager\Decorator;
class MyDecorator
extends \Aimeos\MShop\Common\Manager\Decorator\Base
implements \Aimeos\MShop\Common\Manager\Decorator\Iface
{
private $attr = [
'ip' => [
'code' => 'ip',
'internalcode' => 'mrev."ip"',
'label' => 'Customer IP address',
'type' => 'string',
'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
'public' => false,
],
];
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 );
}
}
Code: Select all
<?php
namespace Aimeos\MShop\Review\Item;
class StandardCustom extends Standard
{
public function getIp() : ?string
{
return (string) $this->get( 'ip', 1 );
}
public function setIp( ?string $value ) : \Aimeos\MShop\Common\Item\Iface
{
return $this->set( 'ip', $value );
}
public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
{
$item = parent::fromArray( $list, $private );
foreach( $list as $key => $value )
{
switch( $key )
{
case 'ip': !$private ?: $item = $item->setIp( $value ); break;
default: continue 2;
}
unset( $list[$key] );
}
return $item;
}
public function toArray( bool $private = false ) : array
{
$list = parent::toArray( $private );
if( ! $private )
{
unset( $list['ip'] );
}
return $list;
}
}
Code: Select all
<?php
namespace Aimeos\MShop\Review\Manager;
class StandardCustom extends Standard
{
// Return custom review item
protected function createItemBase( array $values = [] ) : \Aimeos\MShop\Review\Item\Iface
{
return new \Aimeos\MShop\Review\Item\StandardCustom( $values );
}
}
Re: Add an attribute with a decorator, but private?
Yes, that's OK and the minimum code you need.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star