Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import _media_is_disabled value if 0 #28

Open
matiaso opened this issue Feb 11, 2015 · 12 comments
Open

Cannot import _media_is_disabled value if 0 #28

matiaso opened this issue Feb 11, 2015 · 12 comments
Labels

Comments

@matiaso
Copy link

matiaso commented Feb 11, 2015

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
Copy link
Owner

danslo commented Feb 11, 2015

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 danslo added the Bug label Feb 11, 2015
@willy-ahva
Copy link
Contributor

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
Copy link
Author

matiaso commented Feb 11, 2015

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
#28 (comment).

@willy-ahva
Copy link
Contributor

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
Copy link
Author

matiaso commented Feb 12, 2015

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
#28 (comment).

@willy-ahva
Copy link
Contributor

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

@danslo
Copy link
Owner

danslo commented Feb 12, 2015

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
Copy link
Contributor

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

@danslo
Copy link
Owner

danslo commented Feb 12, 2015

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
Copy link
Contributor

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

@danslo
Copy link
Owner

danslo commented Feb 12, 2015

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 .

@matiaso
Copy link
Author

matiaso commented Feb 24, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants