5

I am trying to convert a \Magento\Sales\Api\Data\OrderInterface object to an array using \Magento\Framework\Api\ExtensibleDataObjectConverter.
I think it is not relevant here but I am using Magento CE 2.1.4

Here is the code I use :

<?php

use Magento\Framework\Api\ExtensibleDataObjectConverter;      

public function __construct(
    [...]
    ExtensibleDataObjectConverter $dataObjectConverter
) {
    [...]
    $this->dataObjectConverter = $dataObjectConverter;
}

public function get()
{
    /** @var \Magento\Sales\Api\Data\OrderInterface $order */
    $order = $this->orderRepository->get($orderId);

    /** @var array $data */
    $data = $this->dataObjectConverter->toFlatArray($order);
}

But here is the error I got :

Getter return type must be specified using @return annotation. See Magento\Sales\Model\Order::getPayment()

If we take a look at \Magento\Framework\Reflection\TypeProcessor::getGetterReturnType() there is this line :

$returnAnnotations = $methodDocBlock->getTags('return');

Which is using \Zend\Code\Reflection\DocBlockReflection::getTags() to list all DockBlock tags and check if there is a return

Now if we take a look at \Magento\Sales\Model\Order::getPayment() here is the actual function declaration :

/**
 * {@inheritdoc}
 */
public function getPayment()
{
    [...]
}

To fix this issue I have to manually add the @return tag to \Magento\Sales\Model\Order::getPayment()

According to https://www.phpdoc.org/docs/latest/guides/inheritance.html#methods the @return tag is subject to the inheritance so I should not have to put the @return tag in addition to the @inheritdoc one.


My questions are simple :

  • Is that a bug?
  • Am I using the wrong method to convert object to array in Magento 2?

1 Answer 1

12

I think you have to use the Interface Type for converting, since as API, it should have all the return types specified

try to call the function like this

$data = $this->dataObjectConverter->toFlatArray($order, [], \Magento\Sales\Api\Data\OrderInterface::class);

If you do not set a type, the class of the actual object will be used: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Api/ExtensibleDataObjectConverter.php#L44

2
  • Good catch this is it! Thank you for your answer Commented Feb 28, 2017 at 9:06
  • @David-, what is the fix for this error in console in IE,Object doesn't support property or method 'from' Commented Nov 26, 2018 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.