0

I am creating my own resources by extending the Magento REST API, and I'm stuck on what object class should be returned on _retrieveCollection() for my API to render properly on both XML and JSON output.

My code is, roughly, as follows:

class Company_Module_Model_Api2_Invoice extends Mage_Api2_Model_Resource
{
    protected function _retrieveCollection()
    {
        //just can't get the proper object to return here. :(    

    }

    protected function _retrieve() {
        $invoice_increment_id = $this->getRequest()->getParam('invoice_id');
        $invoice = Mage::getModel("sales/order_invoice")->loadByIncrementId($invoice_increment_id);
        return $invoice;

        //Mage_Sales_Model_Order_Invoice is a Mage_Sales_Model_Abstract
        //which in turn is a Mage_Core_Model_Abstract
        //Having a Mage_Core_Model_Abstract as a return renders perfectly
        //as json or xml :)

        //Also works with Mage_Sales_Model_Order which is a  
        //Mage_Sales_Model_Abstract which in turn is a
        //Mage_Core_Model_Abstract 
    }

    protected function _create() {
         ....
         //no matter what you return, this doesn't render any output.
    }

    protected function _delete() {
         ...
         //no matter what you return, this doesn't render any output.   
    }

So the question is: What is the return type for _retrieveCollection()?

UPDATE:

@danny-dev-nz is correct with Mage_Sales_Model_Resource_Order_Invoice_Collection (and I guess any Mage_Core_Model_Resource_Db_Abstract)

I realized my problem is Magento versions. This is straightforward on a Magento 1.9.0.1 installation.

protected function _retrieveCollection()
{

     $order_increment_id = $this->getRequest()->getParam('order_id');
     $order = Mage::getModel("sales/order")->loadByIncrementId($order_increment_id);
     $invoices = $order->getInvoiceCollection();
     return $invoices;       

}     

Output on Magento "1.9.0.1" enter image description here

Output on Magento "1.7.0.2" is quirky:

enter image description here

Ardee Aram
  • 198
  • 5

4 Answers4

2

Have you tried Mage_Sales_Model_Resource_Order_Invoice_Collection .

Using factory method Mage::getResourceModel("sales/order_invoice_collection").

Amit Bera
  • 77,456
  • 20
  • 123
  • 237
Danny Dev Nz
  • 623
  • 6
  • 13
1

Just to add, after experimenting and tweaking, I discovered the following data types to be a valid return for _retrieveCollection():

  • Mage_Core_Model_Resource_Db_Abstract
  • String
  • Non-nested array of strings, (member nested array would come out as empty array). array("a","b","c") is fine while array("a",array("b","c")) is not.
  • associative array with string as key, and string as value (objects on key nor value will NOT work)
Ardee Aram
  • 198
  • 5
1

you simply create a Varien_data_collection, fill it up with varien objects and return the collection:

    $collection = new Varien_Data_Collection(); 

    foreach ($products_collection as $product) {

        $prod = $product_model->load( $product->getProductId() );

        $v_product = array(
                    'id' => $prod->getId(), 
                    'name' => $prod->getName(),
                    'price' => $prod->getPrice(),
                );

        $varienObject = new Varien_Object();
        $varienObject->setData($v_product);
        $collection->addItem($varienObject);
    }

    return $collection;
Yehia A.Salam
  • 429
  • 1
  • 9
  • 29
0
retrieveCollection function give return null value.

and function _retrieve() did not give any result as it return as an obeject. you need convert obeject to to array format

Amit Bera
  • 77,456
  • 20
  • 123
  • 237