1

I have an array with a list of products, and I wanna save each of its attributes in a table. Its a list of 25 products, but it saves the first product's attributes 25 times. Here is my code:

$promo = Mage::getModel('novapc_allnations/promotion');
foreach ($return['response']['diffgrdiffgram']['NewDataSet']['Produtos'] as $product) {
    $promo->setName($product['DESCRICAO']);
    $promo->setIdProduct($product['CODIGO']);
    $promo->setPromoPrice($product['PRECOREVENDA']);
    $promo->setDescrtec($product['DESCRTEC']);
    $promo->setCategory($product['CATEGORIA']);
    $promo->setManufacturer($product['FABRICANTE']);
    $promo->setDepartment($product['DEPARTAMENTO']);
    $promo->setPartnumber($product['PARTNUMBER']);
    $promo->setEan($product['EAN']);
    $promo->setWarranty($product['GARANTIA']);
    $promo->setWeight($product['PESOKG']);
    $promo->setResalePrice($product['PRECOREVENDA']);
    $promo->setPriceWithoutSt($product['PRECOSEMST']);
    $promo->setExpireDate($product['DATAVALIDADEPRECO']);
    $promo->setAvailable($product['DISPONIVEL']);
    $promo->setPic($product['URLFOTOPRODUTO']);
    $promo->setStock($product['ESTOQUE']);
    $promo->setNcm($product['NCM']);
    $promo->setWidth($product['LARGURA']);
    $promo->setHeight($product['ALTURA']);
    $promo->setDepth($product['PROFUNDIDADE']);
    $promo->setActive($product['ATIVO']);
    $promo->setSubstTributaria($product['SUBSTTRIBUTARIA']);
    $promo->setProductOrigin($product['ORIGEMPRODUTO']);
    $promo->setAvailableStock($product['ESTOQUEDISPONIVEL']);
    $promo->setUpdatedAt($product['TIMESTAMP']);
    $promo->save();
}

Is there something wrong with my logic?

3

Don't put Object in foreach it's take a time and everytime new object create. use just set $promo->setId(null) it's working. check it.

$promo = Mage::getModel('novapc_allnations/promotion');
    foreach ($return['response']['diffgrdiffgram']['NewDataSet']['Produtos'] as $product) {
        $promo->setName($product['DESCRICAO']);
        $promo->setIdProduct($product['CODIGO']);
        $promo->setPromoPrice($product['PRECOREVENDA']);
        $promo->setDescrtec($product['DESCRTEC']);
        $promo->setCategory($product['CATEGORIA']);
        $promo->setManufacturer($product['FABRICANTE']);
        $promo->setDepartment($product['DEPARTAMENTO']);
        $promo->setPartnumber($product['PARTNUMBER']);
        $promo->setEan($product['EAN']);
        $promo->setWarranty($product['GARANTIA']);
        $promo->setWeight($product['PESOKG']);
        $promo->setResalePrice($product['PRECOREVENDA']);
        $promo->setPriceWithoutSt($product['PRECOSEMST']);
        $promo->setExpireDate($product['DATAVALIDADEPRECO']);
        $promo->setAvailable($product['DISPONIVEL']);
        $promo->setPic($product['URLFOTOPRODUTO']);
        $promo->setStock($product['ESTOQUE']);
        $promo->setNcm($product['NCM']);
        $promo->setWidth($product['LARGURA']);
        $promo->setHeight($product['ALTURA']);
        $promo->setDepth($product['PROFUNDIDADE']);
        $promo->setActive($product['ATIVO']);
        $promo->setSubstTributaria($product['SUBSTTRIBUTARIA']);
        $promo->setProductOrigin($product['ORIGEMPRODUTO']);
        $promo->setAvailableStock($product['ESTOQUEDISPONIVEL']);
        $promo->setUpdatedAt($product['TIMESTAMP']);
        $promo->setId(null);
        $promo->save();
    }
  • It worked! Really faster, too. Thank you very much. Can you explain what setId(null) is for, please? – Stordon Aug 23 '17 at 19:29
  • Your Wel come. Programming logic. – Magento2 Devloper Aug 23 '17 at 19:30
  • it's means when object insert new records it's id set null so don't need to create again. if you only set it to "NULL" Magento will only think you don't want to update the value. – Magento2 Devloper Aug 23 '17 at 19:31
0

That's because you have created $promo object outside for-each loop.

foreach ($return['response']['diffgrdiffgram']['NewDataSet']['Produtos'] as $product) {
    $promo = Mage::getModel('novapc_allnations/promotion');
    $promo->setName($product['DESCRICAO']);
    $promo->setIdProduct($product['CODIGO']);
    $promo->setPromoPrice($product['PRECOREVENDA']);
    $promo->setDescrtec($product['DESCRTEC']);
    $promo->setCategory($product['CATEGORIA']);
    $promo->setManufacturer($product['FABRICANTE']);
    $promo->setDepartment($product['DEPARTAMENTO']);
    $promo->setPartnumber($product['PARTNUMBER']);
    $promo->setEan($product['EAN']);
    $promo->setWarranty($product['GARANTIA']);
    $promo->setWeight($product['PESOKG']);
    $promo->setResalePrice($product['PRECOREVENDA']);
    $promo->setPriceWithoutSt($product['PRECOSEMST']);
    $promo->setExpireDate($product['DATAVALIDADEPRECO']);
    $promo->setAvailable($product['DISPONIVEL']);
    $promo->setPic($product['URLFOTOPRODUTO']);
    $promo->setStock($product['ESTOQUE']);
    $promo->setNcm($product['NCM']);
    $promo->setWidth($product['LARGURA']);
    $promo->setHeight($product['ALTURA']);
    $promo->setDepth($product['PROFUNDIDADE']);
    $promo->setActive($product['ATIVO']);
    $promo->setSubstTributaria($product['SUBSTTRIBUTARIA']);
    $promo->setProductOrigin($product['ORIGEMPRODUTO']);
    $promo->setAvailableStock($product['ESTOQUEDISPONIVEL']);
    $promo->setUpdatedAt($product['TIMESTAMP']);
    $promo->save();
}

Please try above code. It will create new object each time and save data.

Thanks :)

  • It worked! Just one question: I've been told before to never load models in a loop because it kills performance, that would be just for the method load? E.g: $product = Mage::getModel('catalog/product')->load($item->getProductId()); Is it okay to instantiate a model in a loop? – Stordon Aug 23 '17 at 19:08
  • if you want to load product then you can declare model outside loop and then load each product inside loop. For example, $product = Mage::getModel('catalog/product'); foreach ($product_id_array as $product_id){ $product = $product->load($product_id); echo $product->getName() . " - " . $product->getFinalPrice . "<br/>"; } – Abhishek Panchal Aug 23 '17 at 19:12
  • 1
    Never you instantiate a model in a loop, with this solution you will load 25 times a model, you find this fine ?? – PЯINCƏ Aug 23 '17 at 19:15
  • I see! I missinterpreted it then. I thought "you can't load a model inside a loop" meant "you can't instantiate a model inside a loop". My bad! Thanks for your help – Stordon Aug 23 '17 at 19:16
  • Prince, do you mind helping me with some other way, then? This worked fine, but if there is a better and faster way, I'd like to see it, for performance is one of my priorities – Stordon Aug 23 '17 at 19:19
0

As I have said before, Never you you instantiate a model in loop !

I dont tested it but try this one :

$promo = Mage::getModel('novapc_allnations/promotion');
foreach ($return['response']['diffgrdiffgram']['NewDataSet']['Produtos'] as $product) {
    $promo->setName($product['DESCRICAO'])
          ->setIdProduct($product['CODIGO'])
          ->setPromoPrice($product['PRECOREVENDA'])
          ->setDescrtec($product['DESCRTEC'])
          ->setCategory($product['CATEGORIA'])
          ->setManufacturer($product['FABRICANTE'])
          ->setDepartment($product['DEPARTAMENTO'])
          ->setPartnumber($product['PARTNUMBER'])
          ->setEan($product['EAN'])
          ->setWarranty($product['GARANTIA'])
          ->setWeight($product['PESOKG'])
          ->setResalePrice($product['PRECOREVENDA'])
          ->setPriceWithoutSt($product['PRECOSEMST'])
          ->setExpireDate($product['DATAVALIDADEPRECO'])
          ->setAvailable($product['DISPONIVEL'])
          ->setPic($product['URLFOTOPRODUTO'])
          ->setStock($product['ESTOQUE'])
          ->setNcm($product['NCM'])
          ->setWidth($product['LARGURA'])
          ->setHeight($product['ALTURA'])
          ->setDepth($product['PROFUNDIDADE'])
          ->setActive($product['ATIVO'])
          ->setSubstTributaria($product['SUBSTTRIBUTARIA'])
          ->setProductOrigin($product['ORIGEMPRODUTO'])
          ->setAvailableStock($product['ESTOQUEDISPONIVEL'])
          ->setUpdatedAt($product['TIMESTAMP']);
    $promo->save();
    $promo->unsetData();
}

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.