I have successfully created a custom interface.
My model: app/code/Vendor/Samples/Model/Orders.php
class Orders extends \Magento\Framework\Model\AbstractModel implements OrdersInterface
{
protected function _construct()
{
$this->_init('Vendor\Samples\Model\ResourceModel\Orders');
}
public function getSamples()
{
return $this->getData(self::SAMPLES);
}
}
My interface app/code/Vendor/Samples/Api/Data/OrdersInterface.php
interface OrdersInterface
{
const SAMPLES = 'samples';
public function getSamples();
}
The return data is ids serialized like this: ["9","10","11","12","13"]
.
Now my question.
I need to unserialize this data and use this as filter in a collection from another ressource.
Basically i need to extend $orders
with getSamples()
method and apply custom logic like this filter each time i use it.
public function getOrderColors()
{
$samples = $this->serializer->unserialize($order->getSamples());
return $this->colorsFactory->create()->getCollection()
->addFieldToFilter('id', ['in' => $samples]);
}
I can just apply filter manually in block each time i need it, but i think it would be better to implement this logic, so i can use $order->getSamples()
everywhere.
What is the best way of doing this?