Jump to content

Récupérer le dernier id inséré avec le Web Service


Recommended Posts

Bonjour,

 

j'utilise le Web Service pour faire un import de mes catégories et de mes produits.

 

Lorsque j'insère une catégorie, je voudrais récupérer son id après insertion.

 

J'ai trouvé une solution ici https://www.prestashop.com/forums/topic/226655-webservice-how-to-retrieve-the-last-inserted-id-of-a-resource/

$xml = $webService->add($opt);
$result = $xml->children()->children();
$id_category = $result->{'id'};

Par contre, le xml que je récupère est vide, je n'ai pas d'id

object(SimpleXMLElement)#2 (0) {
}

Auriez-vous une solution ?

 

Cordialement,

Thomas

Link to comment
Share on other sites

Bonjour,

Je ne suis pas familier du WebService PrestaShop, mais avez-vous regardé du côté de Db::getInstance()->Insert_ID()?

A la condition que les INSERT soient faits au travers de Db::getInstance() bien sûr, ce qui semble être le cas dans WebserviceRequest.

Cordialement

--

Eric

Edited by erouvier29 (see edit history)
Link to comment
Share on other sites

Malheureusement ça ne fonctionne pas, il me renvoie à chaque fois l'id 0.

 

Ce qui est bizarre c'est que dans la fonction add de PSWebServiceLibrary.php, je vois l'id dans le return en enlevant la fonction parseXML.

 

Je ne comprends pas pourquoi l'id est supprimé ensuite.

 

Merci pour votre aide !

 

Thomas

Link to comment
Share on other sites

Pour info, avec

$xml = $webService->add($opt);

je récupère l'objet

object(SimpleXMLElement)#24 (1) {
  ["category"]=>
  object(SimpleXMLElement)#19 (17) {
    ["id"]=>
    object(SimpleXMLElement)#21 (0) {
    }
    ["id_parent"]=>
    object(SimpleXMLElement)#23 (0) {
    }
    ["level_depth"]=>
    object(SimpleXMLElement)#25 (0) {
    }
    ["nb_products_recursive"]=>
    object(SimpleXMLElement)#26 (1) {
      ["@attributes"]=>
      array(1) {
        ["notFilterable"]=>
        string(4) "true"
      }
    }
    ["active"]=>
    object(SimpleXMLElement)#27 (0) {
    }
    ["id_shop_default"]=>
    object(SimpleXMLElement)#28 (0) {
    }
    ["is_root_category"]=>
    object(SimpleXMLElement)#29 (0) {
    }
    ["position"]=>
    object(SimpleXMLElement)#30 (0) {
    }
    ["date_add"]=>
    object(SimpleXMLElement)#31 (0) {
    }
    ["date_upd"]=>
    object(SimpleXMLElement)#32 (0) {
    }
    ["name"]=>
    object(SimpleXMLElement)#33 (1) {
      ["language"]=>
      object(SimpleXMLElement)#40 (1) {
        ["@attributes"]=>
        array(1) {
          ["id"]=>
          string(1) "1"
        }
      }
    }
    ["link_rewrite"]=>
    object(SimpleXMLElement)#34 (1) {
      ["language"]=>
      object(SimpleXMLElement)#40 (1) {
        ["@attributes"]=>
        array(1) {
          ["id"]=>
          string(1) "1"
        }
      }
    }
    ["description"]=>
    object(SimpleXMLElement)#35 (1) {
      ["language"]=>
      object(SimpleXMLElement)#40 (1) {
        ["@attributes"]=>
        array(1) {
          ["id"]=>
          string(1) "1"
        }
      }
    }
    ["meta_title"]=>
    object(SimpleXMLElement)#36 (1) {
      ["language"]=>
      object(SimpleXMLElement)#40 (1) {
        ["@attributes"]=>
        array(1) {
          ["id"]=>
          string(1) "1"
        }
      }
    }
    ["meta_description"]=>
    object(SimpleXMLElement)#37 (1) {
      ["language"]=>
      object(SimpleXMLElement)#40 (1) {
        ["@attributes"]=>
        array(1) {
          ["id"]=>
          string(1) "1"
        }
      }
    }
    ["meta_keywords"]=>
    object(SimpleXMLElement)#38 (1) {
      ["language"]=>
      object(SimpleXMLElement)#40 (1) {
        ["@attributes"]=>
        array(1) {
          ["id"]=>
          string(1) "1"
        }
      }
    }
    ["associations"]=>
    object(SimpleXMLElement)#39 (2) {
      ["categories"]=>
      object(SimpleXMLElement)#40 (1) {
        ["@attributes"]=>
        array(2) {
          ["nodeType"]=>
          string(8) "category"
          ["api"]=>
          string(10) "categories"
        }
      }
      ["products"]=>
      object(SimpleXMLElement)#41 (1) {
        ["@attributes"]=>
        array(2) {
          ["nodeType"]=>
          string(7) "product"
          ["api"]=>
          string(8) "products"
        }
      }
    }
  }
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...