2012-09-24 21 views
0

我正在開發命令行擴展的操作,仿照內置的cron.php 代碼如下請問使用Magento擴展命令行模式如何使用Mage :: getResourceModel

expansion.php

require 'app/Mage.php'; 
$lowstockCollection = Mage::getResourceModel('reports/product_lowstock_collection') 
->addAttributeToSelect('*') 
->setStoreId($storeId) 
->filterByIsQtyProductTypes() 
->joinInventoryItem('qty') 
->useManageStockFilter($storeId) 
->useNotifyStockQtyFilter($storeId) 
->setOrder('qty', Varien_Data_Collection::SORT_ORDER_ASC); 

執行,將出現以下消息

與該功能對應「非對象上調用一個成員函數getResourceModelInstance()」,如下所示

應用程序/ Mage.php

public static function getResourceModel($modelClass, $arguments = array()) 
{ 
    return self::getConfig()->getResourceModelInstance($modelClass, $arguments); 
} 

顯然,自:: getConfig() - > getResourceModelInstance()的問題

我想問一下如何使用命令行執行法師:: getResourceModel它?

+0

你忘了初始化的Magento 。見http://stackoverflow.com/questions/7145373/magento-fatal-error-call-to-a-member-function-getmodelinstance-on-a-non-obje/7145570#7145570 –

回答

0

Cron.php大部分代碼拷貝進來,我就能用,雖然我不明白他的作用原理:P 程序一般如下

function xxx(){...} 

require 'app/Mage.php'; 

if (!Mage::isInstalled()) { 
    echo "Application is not installed yet, please complete install wizard first."; 
    exit; 
} 

// Only for urls 
// Don't remove this 
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']); 
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']); 

Mage::app('admin')->setUseSessionInUrl(false); 

umask(0); 

xxx;