News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

VmModel && JFactory::getDbo not returning correct query from database [SOLVED]

Started by MDavid, March 09, 2016, 09:19:42 AM

Previous topic - Next topic

MDavid

Hi there,
I made the code below to generate XML and other lists types from my DB.

It works but not returning the correct query's. Without the "if ( $published == true && !empty($nameS) ):" it returns 6 non existent products(I looked it up in the database there is no record of the aforementioned IDs) and half of them are coming as blank values (even if they are NOT blank in the database and they are published)

I am sure that something banal, I just cant find it :)

I have 210 published products and I can only get 136 with this code:
[size=12pt]
<?php

$db 
JFactory::getDbo();
$query $db->getQuery(true);

$query->select($db->quoteName(array('virtuemart_product_id')));
$query->from($db->quoteName('#__virtuemart_products'));
$query->where('published="1"');

$db->setQuery($query);
$results $db->loadObjectList();

$c 1$i 1;
$url =  "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$url rtrim($url,"/");
$escaped_url htmlspecialchars$urlENT_QUOTES'UTF-8' );

foreach (
$results as $virtuemart_product_id => $id) {
  if(
$i <> 1):
  
$productModel VmModel::getModel('Product');
  
$product $productModel->getProduct($virtuemart_product_id);

  
$published $product->published;
  
$nameS $product->mf_name;
  if ( 
$published == true && !empty($nameS) ):
    echo 
'<pre style="width:48%;float:left;margin-right:1%">';
    echo 
'<b>sz:'$c .'</b>'$c++; echo '   p:'$published .'   id:'$virtuemart_product_id .'<br><br>';

    
$product_final_url =  $escaped_url JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=' $product->virtuemart_product_idfalse);
    echo 
'Termék url: <b><a target="_blank" href="'$product_final_url .'">'$product_final_url .'</a></b><br>';
    echo 
'Termék neve: <b>'$product->mf_name .' - '$product->product_name .'</b><br>';
    echo 
'Termék ára: <b style="text-decoration: line-through;">'$product->prices['product_price'] .'</b><br>';
    echo 
'Termék ára: <b>'$product->prices['product_override_price'] .'</b><br>';
    echo 
'Termék rövid leírása: <b>'$product->product_s_desc .'</b><br>';
    
//echo 'Termék leírása: <b>'. $product->product_desc .'</b><br>';
    
echo 'Termék SKU: <b>'$product->product_sku .'</b><br>';
    echo 
'Termék teljes készlet: <b>'$product->product_in_stock .'</b><br>';
    echo 
'Termék rendelve: <b>'$product->product_ordered .'</b><br>';

    
$teljes_keszlet $product->product_in_stock;
    
$rendelve $product->product_ordered;
    
$jelenleg_keszleten $teljes_keszlet $rendelve;
    echo 
'Termék jelenleg készleten: <b>'$jelenleg_keszleten .'</b><br>';
    
//echo 'Termék kategória: <b>'. $product->category_name .'</b><br>';
    //echo 'Termék működési elv: <b>'. $product->virtuemart_custom_id['33']->customfield_value .'</b><br>';
    
echo '</pre>';
  endif;

  endif;
$i++;
}

?>
  [/size]


Joomla: 3.4.8
VM: 3.0.12
David

Milbo

Your code is "horrible".

This "$db->getQuery(true);" wont work in future versions and is from my point of view an academic idea, which never really worked. There is also no need to invent the wheel again, use our search sortSearchListQuery

"$url =  "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";"
Please use JUri or vmUri for that

....

Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

MDavid

Thank you for your kind words.

The lack of professionalism in VM documentation made me (relatively noob in php) to write this code.

I found this with no explanation whatsoever. I will try :)

Thank you for your help.
David

MDavid

For ppl trying to achieve the same, this is how I have done it (I hope there is nothing horrible this time):


<?php
$categoryId 
'uncategorized'// This is my best guess, no mention how to NOT set category to query all
$limit '*'// No doc's neither about how to get all of the products but this works :)

$productModel VmModel::getModel('product');
$ids $productModel->sortSearchListQuery (TRUE$categoryIdFALSE$limit); //The first boolean is the publish state. No idea of the third... 

foreach ($ids as $id) {
  
$product $productModel->getProduct($id);

// From $product you can harvest everything you will ever need. Reverse engineer the default.php of productdetails for keys because again... there is no documentation.

// Example to get the full url of the current product in loop
  
$product_final_url JURI::root(FALSEJRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=' $idFALSE));
  echo 
'Full URL: <a target="_blank" href="'$product_final_url .'">'$product_final_url .'</a>';

}
?>



Happy reverse engineering!

thank you,
David
David

Milbo


function sortSearchListQuery ($onlyPublished = TRUE, $virtuemart_category_id = FALSE, $group = FALSE, $nbrReturnProducts = FALSE, $langFields = array())

For me there is anything documented, except the langFields are not obvious.

FALSE=0, but "false" is not FALSE, but (int)false=0=FALSE (the reason for == or ===)
categoryID=0 => all Products

$ids = $productModel->sortSearchListQuery (TRUE, FALSE, FALSE, FALSE);

or $productModel->sortSearchListQuery (); is enough, because you want to call it with the default params. You may call initialiseRequests before to remove any state set by users.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/