9

I have created Custom Api which returns associativeArray.

  1. If I use associativeArray it's giving error
  2. If I use string[] it's working

    But I want to return associativeArray.

Error: Class "array" does not exist. Please note that namespace must be specified

Example Methods:

 /**
 * Create multi Customers
 *
 * @param \Magento\Customer\Api\Data\CustomerInterface $customer
 * @return string[]
 */
 public function save(\Magento\Customer\Api\Data\CustomerInterface $customer);

save method working fine.

 /**
 * Create multi Customers
 *
 * @param \Magento\Customer\Api\Data\CustomerInterface $customer
 * @return array()|associativeArray()
 */
 public function save1(\Magento\Customer\Api\Data\CustomerInterface $customer);

save1 method returning error.

Error: Class "array()" does not exist. Please note that namespace must be specified

So how can I return associativeArray?

Thanks in advance.

Alex Paliarush
13.8k5 gold badges53 silver badges57 bronze badges
asked Oct 9, 2015 at 5:00
1

2 Answers 2

15

Custom data interface should be created to represent key-value pairs, e.g.

<?php
namespace Vendor\Module\Api\Data;
/**
 * Interface which represents associative array item.
 */
interface AssociativeArrayItemInterface
{
 /**
 * Get key
 * 
 * @return string
 */
 public function getKey();
 /**
 * Get value
 * 
 * @return string
 */
 public function getValue();
}

Then for the new service method it is possible to specify:
@return \Vendor\Module\Api\Data\AssociativeArrayItemInterface[].

Service layer must comply with some rules to be exposed via SOAP and REST. It is impossible to represent associative arrays in WSDL without key-value data interfaces, all complex entities in WSDL must be described using primitive types (but this will make it more difficult for clients written in strictly typed languages to consume such API).

There is one exception supported though, but is highly discouraged and should be used only in very special cases. If return or param type will be specified as mixed, it will be transformed in WSDL into anyType.

answered Nov 19, 2015 at 21:14
4
  • @magentotwo did it work for you? Commented Dec 17, 2015 at 17:53
  • I have some other, So didn't tried. I will back soon.. Thanks for your response Commented Dec 18, 2015 at 3:54
  • Thank you Alex, Your answer give me a idea to solve my question :) you can check the same here magento.stackexchange.com/questions/247801/… +1 for your answer. Commented Oct 25, 2018 at 9:36
  • Nice answered it's useful for API Commented Dec 27, 2019 at 5:31
0

Here is a little trick:

/**
* @return \stdClass[] options
*/
public function getOptions();
answered Oct 11, 2023 at 3:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.