danslo / ApiImport

Boilerplate between the Magento API and ImportExport, so that you can do fast Array/XMLRPC/SOAP based product imports.
244 stars 79 forks source link

Cannot import _media_is_disabled value if 0 #28

Open matiaso opened 9 years ago

matiaso commented 9 years ago

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?

danslo commented 9 years ago

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.

willy-ahva commented 9 years ago

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

matiaso commented 9 years ago

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" notifications@github.com 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.

willy-ahva commented 9 years ago

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

matiaso commented 9 years ago

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 notifications@github.com 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.

willy-ahva commented 9 years ago

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

danslo commented 9 years ago

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;
}
willy-ahva commented 9 years ago

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

danslo commented 9 years ago

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.

willy-ahva commented 9 years ago

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

danslo commented 9 years ago

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

matiaso commented 9 years ago

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.