ApiImport icon indicating copy to clipboard operation
ApiImport copied to clipboard

Cannot import _media_is_disabled value if 0

Open matiaso opened this issue 9 years ago • 12 comments

Hello,

I use Talend to import 1'000 products per API call and I encountered some issues when I tried to import products gallery images.

The code in ApiImport/code/Model/Import/Api/V2.php line 49 set the value of _media_is_disabled to NULL and it causes an SQL error because this value cannot be NULL.

Why is it needed to remove empty values?

matiaso avatar Feb 11 '15 12:02 matiaso

Good question. I will tag @willy-ahva since he wrote that code and I'm not sure what it would break if we removed that bit.

danslo avatar Feb 11 '15 17:02 danslo

Hi guys,

I never update this code but, if this line nullify empty values, try to put (int) 0 in your data. If a value is needed, an empty value will be exactly the same than a NULL and will cause an error. Code is ok, pls update your data ;) It works for me.

Regards, Willy

willy-ahva avatar Feb 11 '15 17:02 willy-ahva

I use the API through a Soap component in Talend, when I send a value like:

_media_is_disabled 0

This is nulled by the trim in the code and therefore causes an error when inserted into the db. Le 11 févr. 2015 18:46, "willy-ahva" [email protected] a écrit :

Hi guys,

I never update this code but, if this line nullify empty values, try to put (int) 0 in your data. If a value is needed, an empty value will be exactly the same than a NULL and will cause an error. Code is ok, pls update your data ;) It works for me.

Regards, Willy

— Reply to this email directly or view it on GitHub https://github.com/danslo/ApiImport/issues/28#issuecomment-73927526.

matiaso avatar Feb 11 '15 18:02 matiaso

Are you sure 0 is passed in the trim ? I ask it because trim(0) returns 0 so I don't really understand. Another fix could be to send "0" instead of 0, it should work.

Regards, Willy

willy-ahva avatar Feb 12 '15 08:02 willy-ahva

Ok, but !trim(0) is true and then the value is set to NULL.

If I set the value to "0" it causes an SQL error as well.

On Thu, Feb 12, 2015 at 9:56 AM, willy-ahva [email protected] wrote:

Are you sure 0 is passed in the trim ? I ask it because trim(0) returns 0 so I don't really understand. Another fix could be to send "0" instead of 0, it should work.

Regards, Willy

— Reply to this email directly or view it on GitHub https://github.com/danslo/ApiImport/issues/28#issuecomment-74037383.

matiaso avatar Feb 12 '15 12:02 matiaso

Ok, I see. You can try to check if (!trim($value) && !is_numeric($value)). If it works pls PR it ;)

willy-ahva avatar Feb 12 '15 13:02 willy-ahva

The following should then also work, and should be a bit cleaner:

$value = trim((string)$object->value);
if (strlen($value)) {
    $return[$i][$object->key] = $value;
}

danslo avatar Feb 12 '15 14:02 danslo

But in this case, a 0 will be cast in "0", and it will result as an SQL error, no ?

willy-ahva avatar Feb 12 '15 14:02 willy-ahva

Just checked this.

<?php

require_once 'app/Mage.php';

Mage::app('admin');

$products = Mage::helper('api_import/test')->generateRandomSimpleProduct(1);

$products[1]['_media_attribute_id'] = Mage::getModel('eav/entity_attribute')
    ->load('media_gallery', 'attribute_code')
    ->getAttributeId();
$products[1]['_media_image']       = '/a.png';
$products[1]['_media_is_disabled'] = '0';   // Notice that it's a string.
$products[1]['_media_position']    = '0';

try {
    Mage::getModel('api_import/import_api')->importEntities($products, 'catalog_product');
} catch (Exception $e) {
    printf("%s: %s\n", $e->getMessage(), $e->getCustomMessage());
}

Works fine for me. No exception thrown in _saveMediaGallery.

danslo avatar Feb 12 '15 15:02 danslo

So it's what I said previously @danslo. Replace 0 by "0" works, and we shouldn't update the code, no ?

willy-ahva avatar Feb 12 '15 15:02 willy-ahva

I think that should work, so that's why these are probably 2 issues:

  • Why do we need to set empty values to NULL, instead of not setting them at all.
  • Why does "0" not work for @aphroz .

danslo avatar Feb 12 '15 15:02 danslo

I think it is related with the fact that I use a Java component to send the data as XML to the API.

If I send something like "0" the data received will be '"0"' and cause an exception when inserted in SQL.

matiaso avatar Feb 24 '15 09:02 matiaso