cancel
Showing results for 
Search instead for 
Did you mean: 

Why and how does this Magento1's saveRewrite function work?

Why and how does this Magento1's saveRewrite function work?

OK, so looking at Magento 1's Mage_Catalog_Model_Resource_Url class, there's a function called saveRewrite. I understand how the core_url_rewrite table works: we add "request paths" which internally redirect to category/product pages based on the rules defined in that table. Let me make an example so this story has its head and tail. Let's say I have a category with an ID 56 and I want a path of "first/second/category-name.html" to take me to that category page. I'd have a row with this data in core_url_rewrite (only the important columns): store_id: 1 category_id: 56 product_id: NULL id_path: category/56 request_path: first/second/category-name.html target_path: catalog/category/view/id/56

 

Everything fine until now. If I type in mydomain.com/first/second/category-name.html, it's fine.

OK, so, let's move on. Let's say i now want my request path to be "third/category-name.html". I run this: Mage::getModel('catalog/url')->refreshCategoryRewrite($categoryId, $storeId, false);

This propagates to Mage_Catalog_Model_Resource_Url class, its function saveRewrite (documentation here: http://magento-api.cekuj.net/source-class-Mage_Catalog_Model_Resource_Url.html#289-323 ).

There, I'd have $rewrite as: Array ( [store_id] => 1 [category_id] => 56 [product_id] => [id_path] => category/56 [request_path] => third/category-name.html [target_path] => catalog/category/view/id/56 [is_system] => 1 )

which is exactly as it is supposed to be. However, in this code snippet from Magento's core function saveRewrite (the one from the previous link), it says:

 

if ($rewrite && $rewrite->getId()) {

         if ($rewriteData['request_path'] != $rewrite->getRequestPath()) {

                   $where = array('target_path = ?' => $rewrite->getRequestPath());

                   if ($rewrite->getStoreId()) {

                            $where['store_id = ?'] = (int)$rewrite->getStoreId();

                   }

                  $adapter->update( $this->getMainTable(), array('target_path' =>                  $rewriteData['request_path']), $where );

        }

}

 

What's going on here? Magento uses this to create a query which will update the core_url_rewrite table (change the request path value from the old one to the new one) - OR AT LEAST IT SHOULD DO THAT? However, in both the WHERE sql query clause and in the parameters part, it uses the column "target_path" but associates its values with those of "request_path". Basically, what this would do is generate a query which would look like this:

UPDATE core_url_rewrite SET target_path = ? WHERE (target_path = 'first/second/category-name.html') AND (store_id = 1)

 

Of course, the ? part would resolve to 'third/category-name.html'.

So, what is going on here? How is this supposed to work? Of course, this does not work for me, so I was digging up a bit, going from function to function and saw this in the end. I just don't understand why did they use "target_path" instead of "request_path" here, which would basically solve everything. What am I missing?

 

Thanks in advance,