10个最坏Magento的用法(10 worst Magento practices)
来源:互联网 发布:淘宝用什么软件记账 编辑:程序博客网 时间:2024/06/11 03:08
Wrong Magento implementation can have critical consequences as dropping conversion rates. How to save your eCommerce before bad practices? It should be in our best interest to optimize code and therefore improve user experience. PHP code issues can be easier to find and fix than infrastructure ones.
I will introduce you the 10 worst Magento development practices followed by detailed examples. I picked the most common and the most important ones. I hope this article will help many to save their eCommerce before bad practices and to optimize their code.
1. Overriding core files
Magento uses nice event hooking system, following the Observer Pattern, allowing additional functionality to be plugged in and out without modifying the core code.
Very often developers rewrite a core file when there’s a possible way to accomplish the functionality with an event observer. Magento provides a lot of events that are dispatched at different times. All of these events can be observed, and the objects provided by the events can be modified.
There’s is no possible way to rewrite the same core file more than once without creating extends chain. By using event observers multiple modules can exist at the same time without conflicting — many modules can observe the same event.
2. Collections – query results limitation
In order to improve code performance and scalability remember to apply limitation on the collection’s query results — it’s much better to do **$collection->getSize() instead of $collection->count() or count($collection)**
because that ways, all of the items will be loaded from the database and than iterated.
3. Memory – fetching large result set
Method fetchAll() used to fetch and iterate over larger result sets will lead to a very long execution time, moreover PHP will probably run out of memory. The better solution is to fetch row by row using the fetch() method. For example:
Wrong:
$rowSet = $this->_getReadAdapter()->fetchAll($select);foreach ($rowSet as $row) { //process row}
Good:
$query = $this->_getReadAdapter()->query($select);while ($row = $query->fetch()) { //process row}
4. Counting array
PHP Function count() is fast but when used in a loop this changes really severely. Calculating the size of an array on each iteration of a loop, if the array or collection contains a lot of items, will result in much longer execution time. Counting size should be done outside the loop not on each iteration.
Wrong:
for ($i = 0; $i < count($array); $i++){ // do something}
Good:
$size = count($array);for ($i = 0; $i < $size; $i++){ // do something}
5. SQL queries inside a loop
Very common bad practice is to load Magento models and process it inside a loop. Running SQL query is very expensive operation, and doing it in a loop tends to make it even worse. Instead of doing that we could use data collections to load models and then process it. We also need to be careful when working with collection to do not exceed memory.
Wrong:
foreach ($this->getProductIds() as $productId){ $product = Mage::getModel('catalog/product')->load($productId); $this->processProduct($product);}
Good:
$collection = Mage:getResourceModel('catalog/product_collection') ->addFieldsToFilter('entity_id', array($this->getProductIds())) ->addAttributeToSelect(array('name'));foreach ($collection as $product){ $this->processProduct($product);}
6. Models loading
We need to remember that each time of executing load() method, separate query’ll be executed on a database. Also, loading multiple models separately, slows down our code. Models should be loaded only once, with a single SQL query.
Wrong:
$attr = Mage::getModel(‘catalog/product’)->load($productId)->getAttr();$sku = Mage::getModel(‘catalog/product’)->load($productId)->getSku();
Good:
$product = Mage::getModel(‘catalog/product’)->load($productId);$attr = $product->getAttr();$sku = $product->getSku();
We also don’t always need the whole model to be loaded — we can do it only when its necessary. In other cases, load only attributes which are needed.
$productId = Mage::getModel(‘catalog/product’)->getIdBySku($sku);
7. Cron misconfiguration
Many developers use Daemons but the most common way to schedule tasks is through Cron jobs. Magento has a cron.php file that allows developers to add a crontab node to modules configuration. You must ensure that your system has a cronjob for doing it that way.
<crontab> <jobs> <catalogrule_apply_all> <schedule> <cron_expr>0 1 * * *</cron_expr> </schedule> <run><model>catalogrule/observer::dailyCatalogUpdate</model> </run> </catalogrule_apply_all> </jobs></crontab>
8. Invalid cache usage
Speed up your Magento project by caching View parts. The HTML output of a block can be saved in the Magento cache. There’re nearly no blocks saved in the default Magento store, except the frontend and admin headers. The cache management has to be written in the constructor of the Block:
class {NS}{Module}_Block{View} extends Mage_Core_Block_Template {
protected function _construct() { $this->addData(array( 'cache_lifetime' => 120, 'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG), )); } }
The output code will be saved in a cache until the “Product” cache will be deleted. If cache_lifetime is equal to false, it means that the cache will never expire.
9. Theme for mobile browsers
Good practice is to use optimized templates for mobile browsers. Magento can check Browser Agent and than serve a different theme, optimized for mobile. Magento allows to add exception to the most of the configuration parameters. It takes a regular expression string and compare it with the user’s browser agent.
You can use it to check what browser user’s using and set a different package, template, skin or layout. In other words, you can set a different them for mobile browsers.
Doing it is simple, just go to System -> Configuration -> General -> Design -> Package and Themesection and set-up appropriate settings.
10. Image Optimization
If you want to serve different images for mobile site, always declare width and height for them. The recommended image dimensions for mobile Magento store are:
* Product image – 250px / 250px * Thumbnail – 120px / 120px
Don’t make the dimension larger and make sure that all your product images are of the same dimensions for height and width.
Photo by Robert Agthe
- 10个最坏Magento的用法(10 worst Magento practices)
- magento翻译的各种用法
- Magento
- magento
- SQL Server Worst Practices
- magento cron的用法 ,以及 null过滤
- magento - 使用后台设置的时间用法
- magento多个首页的问题解决
- magento 创建第一个自己的模块
- magento -- magento的系统要求(安装前提)
- magento sql 用法
- 10个必装Magento免费插件
- 通过magento后台的magento connect安装magento extension
- magento -- 如何设置使用magento的优惠券
- magento -- magento做的站点参考推荐
- magento -- 优化magento速度时想起的
- magento -- 优化magento速度时想起的
- magento -- seagull与magento的共同概念
- 疯狂食材 总结
- Image转BufferedImage
- Multiply Strings
- HDU 5505 GT and numbers
- 读后有感 - 媒体称教育减负加剧阶层分化 贫民子弟受损
- 10个最坏Magento的用法(10 worst Magento practices)
- 数据包接收系列 — IP协议处理流程(二)
- ArcGIS数据生产与精细化制图之中国年降水量分布图的制作
- bp神经网络
- 正则表达式 各种校验
- maven项目转换成dynamic项目
- 让Mac发出iPhone一样的充电提示音
- Netfilter的使用和实现
- 11384 - Help is needed for Dexter