MrBaseball34 Posted September 9, 2011 Share Posted September 9, 2011 Got this code in an import script. I'm trying to "Prestashopize" this script by using only Prestashop classes to perform the operations. // this is how the lookup_product function is called to determine if the product exists already if (strlen($unique_key) > 0) { $productID = lookup_product($db, $unique_key); } else { $productID = 0; } // if (strlen($title) > 0) // Returns 0 if product name does not exist; returns product ID if product name exists // $db is a persistent Database class being passed into the function // query, numRows, and getResult are functions of the Database class // $prefix is store in config.php function lookup_product($db, $unique_key){ require('config.php'); $id = 0; $sql = "SELECT p.id_product as id FROM " . $prefix . "product p ". "INNER JOIN " . $prefix . "feature_product pfp ON pfp.id_product = p.id_product ". "INNER JOIN " . $prefix . "feature_value_lang pfvl ON pfvl.id_feature_value = pfp.id_feature_value ". "WHERE pfvl.id_lang = " . $defaultLanguageID . " AND pfvl.value = '".$unique_key."' ". "LIMIT 1"; $result = $db->query($sql) or die('error, lookup_product query failed'.$sql."<br>".mysql_error()); $num = $db->numRows($result); if($num > 0){ $id = $db->getResult($result,0,"id"); } unset($result); unset($sql); unset($num); return $id; } Essentially I'm looking for the product based on the unique key passed in. This value is also stored in the reference column as well as in ps_feature_value_lang. So, I'm thinking of using the Product::getByReference() function. This is really only used to tell if the product already has been imported. Could I use this code, instead? if (strlen($unique_key) > 0) { $product = new Product; $product->getByReference($unique_key); $productID = $product->id_product; } // if (strlen($title) > 0) Link to comment Share on other sites More sharing options...
Burhan BVK Posted September 9, 2011 Share Posted September 9, 2011 Why don't you use sql directly to check. if($id_product=Db::getInstance()->getValue('SELECT id_product FROM '._DB_PREFIX_.'product WHERE reference="'.pSQL($reference).'"')){ } 1 Link to comment Share on other sites More sharing options...
MrBaseball34 Posted September 10, 2011 Author Share Posted September 10, 2011 While I never though of doing it like that, it looks doable. Link to comment Share on other sites More sharing options...
ircf Posted May 30, 2012 Share Posted May 30, 2012 (edited) Hello, I have the exact same issue : I develop an product import module with something like : $product = ProductCore::getByReference($ref); if ($product){ // update existing product }else{ // Insert new product } This works great, except when I try to execute this about 30K times in a loop. The program exits with a "Fatal error : Allowed memory size..." The problem might be that this method uses DB::Instance->getRow("...") with cache enabled, but I'd like to avoid hacking PS... Besides ProductCore::getByReference() is marked as deprecated, but I couldn't find any replacement method. I think I'll try to replace it with something like $product = new ProductCore(DB::Instance->getValue("...")) but this seems less "Prestashopized" to me. I don't understand why this method wasn't simply optimized instead of being deprecated... Or would it be another solution ? Thank you. Edit : The memory issue can be avoided by flushing cache periodically with Cache::getInstance()->flush(); but I suppose it's better not to use cache at all in my case, or maybe if you need to query for a product many times, like when importing images, you have to check the (same) product for each image. Edited May 30, 2012 by ircf (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now