1

Here is the collection I have as an output :

Array
(
    [0] => Array
        (
            [item_id] => 1
            [rma_id] => 1
            [product_sku] => 24-MB02
            [subsidiary] => CA
        )

    [1] => Array
        (
            [item_id] => 2
            [rma_id] => 2
            [product_sku] => 24-MB02
            [subsidiary] => DE
        )

    [2] => Array
        (
            [item_id] => 3
            [rma_id] => 3
            [product_sku] => 26-MB02
            [subsidiary] => DE
        )

)

In this, I want to have records with the same SKU but different subsidiaries only.

Desired output,

Array
(
    [0] => Array
        (
            [item_id] => 1
            [rma_id] => 1
            [product_sku] => 24-MB02
            [subsidiary] => CA
        )

    [1] => Array
        (
            [item_id] => 2
            [rma_id] => 2
            [product_sku] => 24-MB02
            [subsidiary] => DE
        )
 )

You see the same product_sku but a different subsidiary.

here is the code.

$rmaCollection = $_objectManager->create('Mirasvit\Rma\Model\ResourceModel\Post\CollectionFactory'); //avoid object manager use while development.

$collection = $rmaCollection->create()->load();

print_r($collection->getData());

Any thoughts on this ?

1

1 Answer 1

0

I'm not familiar with this 3rd party extension, but I'll talk about how to it work in general

// this part means you create collection factory
$factory = $_objectManager->create('Mirasvit\Rma\Model\ResourceModel\Post\CollectionFactory');

// this part means you create new collection instance
$collection = $factory->create();

// next you may need to add filters
$collection->addFieldToFilter('field', ['eq' => 'some_value']);

// next you can set your order
$collection->setOrder('subsidiary', \Zend_Db_Select::SQL_ASC);

// next you can set some limits, by default without limits
$collection->setPageSize(100);

// this means you load 
// you don't need to load data if you are going to use getData method
// collection will be loaded automatically when you'll iterate it
$collection->load();

// You can use collection in 2 ways:
// 1. Use objects
foreach ($collection as $item) {
    // do something with item
    // item is instance of related model
}

// 2. Use RAW with getData method
foreach ($collection->getData() as $item) {
    // do something with item
    // item is array
}

// Note: If you will be use both ways on the same collection the data will be loaded twice
4
  • Hi Victor, I am trying to prevent having duplicate values for product SKU in list considering only one row of SKU for one subsidiary ? Jun 12 at 8:54
  • In this case better to create your own collection and specify request. As workaround you can use $collection->getSelect() method to get Select object and use some like ->group('product_sku'). Anyway, you didn't specify your end goal. Jun 12 at 9:38
  • Can we have a quick chat? Jun 12 at 12:17
  • advocat [at] gmail.com Jun 13 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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