Show the catalog is too slow in Symfony2 - php

I'm working on an app that has a product catalog with a n-level category tree.
I have the next structure:
Category 1
--Category 1.1
----Category 1.1.1
------Product 1.1.1.1
------Product 1.1.1.2
----Product 1.1.1
--Category 1.2
----Product 1.2.1
--Product 1.1
And I need something like a flat array:
Category 1
Category 1.1
Category 1.1.1
Product 1.1.1.1
Product 1.1.1.2
Product 1.1.1
Category 1.2
Product 1.2.1
Product 1.1
But the problem is that I need too much queries and too much time to generate this (I have 700 products and 200 categories more or less and it takes about 846 queries and 2100 ms to execute them). I can add more joins, but the execution time grows significantly. I'm working with translatable entities.
Any idea to minimize the queries number and the execution time? Categories has a self-relationship and a relationship with products.
I think that an option can be to get all categories and make a ordered tree and later redo the flat array again, but I don't know if it could be the best option...
Thank you in advance for any help!
EDIT:
This is my function to generate the "flat" array.
public function generateCategoriesTree($categories, $result = array()) {
$tempResult = array();
if(count($categories)) {
foreach($categories as $k => $v) {
$tempResult[] = $v;
if(count($v->getCategories())) {
$temp = $this->generateCategoriesTree($v->getCategories(), $result);
foreach($temp as $kk => $vv) {
$tempResult[] = $vv;
}
}
}
}
return array_merge($result, $tempResult);
}
But the problem is not here, the problem is when I trying to acces to the second level of the tree, when call to the method getcategories, that execute a query in the database.
UPDATE
With this solution, I reduced the number of queries from 700 to 100 and the execution time from 2100ms to 350ms. I think that it could be enough for now, becouse this actions is used a couple per hour.
if ($this->isGranted('ROLE_EMPLOYEE') || $currentUser->isXXXXXXSeller()) {
$XXXXXXCategories = $em->getRepository('CatalogBundle:Category')->getMainCategoriesByBrand(1);
if(is_array($XXXXXXCategories)) {
$categoriesXXXXXX = $this->get('core_tools')->generateCategoriesTree($XXXXXXCategories);
$categories = new ArrayCollection(
array_merge($categories->toArray(), $categoriesXXXXXX)
);
}
}
$categoriesToShow = array();
$products = $em->getRepository('CatalogBundle:Product')->getAllOrderedByPosition();
if(is_array($products) && is_array($categories)) {
foreach($categories as $category) {
$categoriesToShow["c_".$category->getId()] = array(
"category" => $category,
"products" => array(),
);
}
foreach($products as $product) {
if($product->getCategories()) {
foreach($product->getCategories() as $productCategory) {
if(array_key_exists("c_".$productCategory->getCategory()->getId(), $categoriesToShow)) {
$categoriesToShow["c_".$productCategory->getCategory()->getId()]['products'][] = $product;
}
}
}
}
}

If the catalog doesn't change very often (ie: more than every few seconds) - then do the hard work once, and then cache the output ready to be used instantly.
When the catalog changes - rebuild the cache.

If you are using MySQL you might like to consider the "Nested Sets" model to store your category hierarchy. The complexity of your SELECTs will usually stay constant no matter how many levels your category tree has. There are Doctrine extensions available that help you setting it up (e.g. https://github.com/blt04/doctrine2-nestedset).
If you are using a DBS supporting hierarchical queries you can consider using them (https://en.wikipedia.org/wiki/Hierarchical_and_recursive_queries_in_SQL).

Related

Adding categories to product prestashop

So I've been dealing with this problem for quite some time and can't find a clear solution. So basically I'm adding new products directly into db using Product object. For now it went well but I can't manage to link new products with a category.
$cat_ids = [];
foreach ($value['kat_naziv'] as $cat_val) {
$cat_ids[] = (int)$luceed->selectCategoryIds($cat_val)[$cat_val]['id_category'];
}
$product->id_category = 3;
$product->id_category_default = 3;
$product->save();
$product->addToCategories($cat_ids);
So basically $cat_ids is an array of integers that i'm getting from db where name is something i pass as a parameter to selectCategoryIds;
What is the problem here why it wont associate newly created product with categories i give to it
After creating your new product ( i.e $product = new Product() ). You can assign categories to product using.
$product->updateCategories($category_array);
where
$category_array = array("0" => "2", "1" => "3", "4" => "6"...... );
#FMEModule That's exactly what i did there but i've filled the array with the id's of categories from the database
Anyways I ended up writing my own queries for associatting products with categories
Version (1.6)
I found the following bug in Product.php, in addToCategories, search for if (!in_array($new_id_categ, $current_categories)) (line 964),
notice that the if is missing {} - add them, and the problem is solved:
foreach ($categories as $new_id_categ) {
if (!in_array($new_id_categ, $current_categories)) {
if ($position == null) {
$position = (int)$new_categ_pos[$new_id_categ];
}
$product_cats[] = array(
'id_category' => (int)$new_id_categ,
'id_product' => (int)$this->id,
'position' => $position,
);
}
}
Prestashop developer LOVEs omitting {} after if and foreach - this is supper annoying and bug prone.
This issue is fixed in the repo:
https://github.com/PrestaShop/PrestaShop/blob/1.6.1.x/classes/Product.php
NOTE: this solution solves the bug in the following scenario - a product that is already link to a category is link to another category (while keeping the original category)
although I'm not sure if this is the scenario in the question.

PHP & Mysql tree navigation menu with unlimited sub categories

Im looking for a way to extract the data from the mysql database to create an unlimited subcategry menu.
The menu has about 1500 categories in it and they are stored into the database table (menu) in the following format :
category_id , category title , parent_id
i want to do 1 mysql query to put all the data into an array.
$menu_list =array();
$query = mysql_query("SELECT * FROM menu ORDER BY category_id ASC");
if(mysql_num_rows($query) != 0) {
while($row = mysql_fetch_array($query)){
$category_id = $row['category_id'];
$title = $row['category_title'];
$parent_id = $row[parent_id'];
$menu_list[] = array($category_id,$title,$parent_id);
}
}
That is the code that i am currently using to turn the data into the array.
I then need to loop through the array to build a menu up.
I will then hide all subcategories and expand with jquery ( I can do this bit no problem )
The problem i have is going through the array and displaying the data in the right order.
The menu has unlimited subcategories so it will probably need to be a recursive function to retrieve the data.
I have folllowed alot of the examples on here but then i get lost how to display the data..
i now need to end up with :
main item
sub cat
sub cat
sub cat
sub cat
main item
sub cat
main item
main item
sub cat
sub cat
sub cat
sub cat
sub cat
etc...
with each category in its own div
I will then need to be able to edit each category (ie title name and position if need be)
then if i change where a sub category is based a quick page refresh will reload the menu in the new order..
eventually i would like to make this into a drag and drop but that will be at a later date.
I hope this has explained it enough..
Thanks in advance..
So the problem has come back to haunt me again...
My method of ajax calling another mysql select was ok for the CMS section as it was only used from time to time.
But now we have got to the front end.
Each time the page is viewed the first 3 levels of the menu are pulled using numerous mysql requests.
As the categories are getting bigger this is now resulting in 1600+ categories being pulled numerous times each, and even at 1000 visits a day will result in more than a 1000000 sql requests a day.
So i think i will definately need to pull the categories into an array, and then recursively go through the array.
I have looked at the solutions above and they seem to work on paper but not in practise.
If anyone can attempt to give me another solution it would be appreciated..
just to recap in my db i have : id , category_name , parent_id
I am now using PDO with mysql and prepared statements for added security..
Thanks in advance..
Here is the code what you need
category_id AS id , category title AS kategori and parent_id AS kid
function countsubcat($pid)
{
$r=mysql_query("select count(kid) AS say from meskat where kid='$pid' limit 1");
$rw=mysql_fetch_array($r);
return $rw['say'];
}
function listmenu($pid = 0)
{
$res = mysql_query("select id,kategori,kid from meskat where kid='$pid'");
while($cat=mysql_fetch_array($res))
{
echo '<li>';
print''.$cat['kategori'].'';
if(countsubcat($cat['id'])>0)
{
print'<ul>';
listmenu($cat['id']);
print'</ul>';
}
echo '</li>';
}
}
echo '<ul>';
listmenu(0); //starting from base category
echo '</ul>';`
In MySQL, this will require many queries, one for each level plus one at a minimum.
you grab all the top-level categories, use them as root nodes for a forest (a group of trees).
You grab all of the categories that are children of any of them.
Add each of these categories as children to the parent node
If there are still children, go to step two, otherwise stop.
What you end up with a tree that you do a depth-first walk through to convert to some (presumably) html representation (e.g. nested lists).
There are some optimisations you can perform. If you sort the child levels by parent ID, then you can assume continuous blocks of children, meaning you don't have to do as many lookups to find the correct parent, just check for a parent id change.
If you maintain a list of the current lowest level of categories, indexed by ID, you can speed up forest creation.
If you overlap steps 2 and 4, then you only do n+1 queries (where n is the number of levels), so checking for more children is also grabbing the next level of children.
In terms of memory, this will use the memory required for the tree, plus lowest level lookup table and a current parent id.
The building algorithm it fairly fast, scaling linearly with the number of categories.
This method also successfully avoids corrupted sections of data, because it won't grab data that have cycles (no root node).
I also suggest using a prepared statement for the children query, since MySQL will compile and cache the query, it will speed up the operation by only sending new data across the wire.
If you don't understand some of the concepts, I suggest hitting up Wikipedia as I have tried to use standard terms for these.
Can you alter the table structure? In that case you could take a look at the Nested Set Model (link includes description and implementation details, scroll down to The Nested Set Model). Node insertion and removal becomes more complex, but allows you to retrieve the whole tree in just one query.
If you can assume that for any category category_id > parent_id is true, the following recursive functions to represent the menu as a multi-level array and render it as a nested HTML list would work:
$menu = array();
$query = mysql_query("SELECT category_id, category_title, parent_id FROM menu ORDER BY category_id ASC");
if(mysql_num_rows($query) != 0) {
while($row = mysql_fetch_assoc($query)) {
if(is_null($row['parent_id']))
$menu['children'][] = $row;
else
add_to_menu(&$menu,$row);
}
}
function add_to_menu($menu,$item) {
if(isset($menu['children'])) {
foreach($menu['children'] as &$child) {
if($item['parent_id'] == $child['category_id']) {
$child['children'][] = $item;
} else {
add_to_menu(&$child,$item);
}
}
}
}
function render_menu($menu) {
if(isset($menu['children'])) {
echo '<ul>';
foreach($menu['children'] as &$child) {
echo "<li>";
echo $child['category_id']." : ".$child['category_title'];
if(isset($child['children'])) {
render_menu(&$child);
}
echo "</li>";
}
echo '</ul>';
}
}
render_menu($menu);
So after various attempts at different solutions i actually have used jquery and a ajax load to retreive the next level.
With mine main categories all have a parent of 1 - shop i do a mysql scan for all categories with 1 as the parent id.
This will then display the main categories. Attached to each main category i have added a folder icon if they have any sub categories.
I have also inserted a blank div with an id of that category.
When the folder is clicked it does an ajax call to a php script to find all categories with that parent. These are appended to the parents_id div.
This inturn adds the category (with a css padding to indicate a sub category) and again a folder sign if it has sub categories as well as another blank div with the category_id.
You can continue this for as many categories and sub categories as the client requires.
I took it a stage further and added an edit icon to category/subcategory so categories can be moved or changed.
If you would like some sample code for this let me know...
Actually, you need very little code to build a tree.
You also only need one query.
Here is what I came up with (I use Drupal's database but it is explicit enough to understand):
$items = db_select('menu_items', 'mi')
->fields('mi')
->orderBy('position')
->execute()
->fetchAllAssoc('id', PDO::FETCH_ASSOC);
// Example of result. The ID as key is important for this to work.
$items = array(
3 => array('id' => 3, 'parent' => NULL, 'title' => 'Root', 'position' => 0),
4 => array('id' => 4, 'parent' => 3, 'title' => 'Sub', 'position' => 0),
5 => array('id' => 5, 'parent' => 4, 'title' => 'Sub sub', 'position' => 0),
6 => array('id' => 6, 'parent' => 4, 'title' => 'Sub sub', 'position' => 1),
);
// Create the nested structure. Note the & in front of $item.
foreach($items as &$item)
if($item['parent'])
$items[$item['parent']]['sub items'][$item['mid']] =& $item;
// Now remove the children from the root
foreach($items as $id => $item)
if($item['parent']) // This is a child
unset($items[$id])
At this point, all you need is a recursive function to display the menu:
function print_menu($items) {
echo '<ul>';
foreach($items as $item) {
echo '<li>';
echo '' . $item['title'] . '';
if(!empty($item['sub items']))
print_menu($item['sub items']);
echo '</li>';
}
echo '</ul>';
}

Create a Dynamic Nav on the Fly

I'm looking for the best way to create a complex navigation element on the fly. I have all of the elements in a database (title, id, parentId) and I want to efficiently take them out of the DB and display them correctly. I also want to collapse all of the navigation elements that aren't active. So if I was browsing through "Sofas" I wouldn't see "Chandeliers" or any of the categories under lighting but I would see "Lighting".
This is what I want the final product to look like:
Furniture
Living Room
Sofas
Chairs
Ottomans
Bedroom
Beds
Nightstands
Lighting
Chandeliers
Floor Lamps
Sconces
Rugs & Textiles
Contemporary
Vintage
My current method is
write one SQL query that pulls down all of the category names, ids, and parent ids
Iterate through the categories and put into a sorted multi-dimensional array with child categories stored under their parents.
Iterate through the new array and add another entry to mark the appropriate categories as open (all categories are closed by default)
iterate through the array and write HTML
I'm trying to to this with as few interations as possible and I'm sure the code I have right now is inefficient. Especially step 2 I iterate through the array several times. There has to be a general solution to this (common?) problem.
Consider adding a new field to your database table: level.
Main-categories will have level 0.
Sub-categories will have level 1.
Sub-sub-categories will have level 2.
etc.
This trick will help you to know which sub-categories to disable without 2nd iteration of the array.
I believe this is the perfect place to generate your html code using recursion.
I used this function a while ago. It is working with a multi-dimensional array (tree)
function buildMenu($menu_array, $is_sub=FALSE) {
$attr = (!$is_sub) ? 'id="menu"' : 'class="submenu"';
$menu = "<ul $attr>\n";
foreach($menu_array as $id => $elements) {
foreach($elements as $key => $val) {
if(is_array($val)) {
$sub = buildMenu($val, TRUE);
}
else {
$sub = NULL;
$$key = $val;
}
}
if(!isset($url)) {
$url = $id;
}
$menu .= "<li>$display$sub</li>\n";
unset($url, $display, $sub);
}
return $menu . "</ul>\n";
}
echo buildMenu($menu_array);
This adds css properties too. If you wish to mark the currently active page you can use the strpos() function to find your current url. If you need some more functionality you can easily add them to buildMenu()
Using level as mentioned in the answer above will help too. If you were using the nested set model in your database I could also help you with my query which is a single select returning the whole menu data.

Sorting Silverstripe SiteTree by depth

I want to get all of the SiteTree pages of a Silverstripe website and then sort them by depth descending. By depth I mean the number of parents they have.
This is already done to some extent by the Google Sitemaps Module. Except it doesn't go past a depth of 10 and doesn't calculate for pages hidden from search: https://github.com/silverstripe-labs/silverstripe-googlesitemaps
Looking at the Google Sitemaps Module module it appears easy enough to count the number of parents of a page: ( /code/GoogleSitemapDecorator.php - line 78)
$parentStack = $this->owner->parentStack();
$numParents = is_array($parentStack) ? count($parentStack) - 1: 0;
But what is the best way to sort the SiteTree using this calculation?
I'm hoping there's an easier way than getting all of the SiteTree, appending the depth, and then resorting.
The function below will get all published pages from the database, checks they are viewable and not an error page, finds out how many ancestors they have and then returns a DataObjectSet of the pages ordered by depth (number of ancestors).
public function CustomPages() {
$filter = '';
$pages = Versioned::get_by_stage('SiteTree', 'Live', $filter);
$custom_pages = new DataObjectSet();
if($pages) {
foreach($pages as $page) {
if( $page->canView() && !($page instanceof ErrorPage) ) {
$parentStack = $page->parentStack();
$numParents = is_array($parentStack) ? count($parentStack) - 1 : 0;
$page->Depth = $numParents;
$custom_pages->push($page);
}
}
}
$custom_pages->sort('Depth', 'DESC');
return $custom_pages;
}
If you're using PostgreSQL you can also use a single database query. In our effort to speed up SilverStripe (2.4), we've replaced the getSiteTreeFor() with a single database query, see:
http://fvue.nl/wiki/SilverStripe:_Replace_getSiteTreeFor_with_single_query
The breadcrumb field contains an array of ID's on which you can sort.

magento memory problem, cannot unset objects

i am writing a magento product exporter, that writes a couple of attributes into a csv file. one attribute is called the "category string" and its method looks like:
...
foreach($products as $_product) {
...
$productId = $_product->getSku();
$productCategory = getCategoryString($_product['category_ids']);
...
}
...
function getCategoryString($numbers) {
$catString = '';
$catModel = Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
$ex = explode(',', $numbers);
foreach ($ex as $i => $e) {
if ($i > 0) {
$catString .= $catModel->load($e)->getName();
if ($i < (count($ex)-1))
$catString .= ' > ';
}
}
$ex = NULL;
$numbers = NULL;
$catModel->unsetData();
unset($catModel);
$catModel = NULL;
return $catString;
}
but after each iteration the method call costs about 1MB for each product and i have about 9000 products! i cannot clean up the $catModel variable! the $catModel = NULL and unset($catModel) lines have no effects. what am i doing wrong? how can i force to unset the object?!
We had the same problem with a cron for Magento, I know that it isn't the best way to do it but we needed to do it quickly.
Our solution was creating a new PHP file with the necessary code to do one single operation. From magento we get a product list and then call with exec() to this external PHP file product by product.
Something like this:
foreach($products as $_product) {
...
exec("do_the_work.php {$_product->getSku()}");
...
}
Hope it helps.
so your script would be so much cooler if you
get all catalog ids from all the products you have as an array
load the category collection with IN() clause with the category names you need
foreach your product collection one more time and select the neccecary category id's from preloaded collection with just the right elements
profit $$$$ cause you are not using memory like its unlimited for you
much much cooler thing would be joining the category names directly to product collection, that would consume even less resources

Categories