初始资源模型不起作用 - 返回非对象
-
16-10-2019 - |
题
我正在尝试扩展企业pagecache模块的爬网部分,但是,我遇到以下错误,我相信这与行$ this-> _ init('vmr_crawler/crawler')有关;在我的模型中找到,我之所以这么说,是因为如果我将vmr_crawler切换为Enterprise_pagecache,它可以工作...这是错误
Fatal error: Call to a member function getUrlsPaths() on a non-object in /jlr/sites/mage/app/code/local/VMR/Crawler/Model/Crawler.php on line 37
任何想法,片段,评论都非常感谢!非常感谢您,我在下面发布了所有代码。
我的模块配置
app/etc/modules/VMR_Crawler.xml
我的模块配置代码
<?xml version="1.0"?>
<config>
<modules>
<VMR_Crawler>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Core/>
</depends>
</VMR_Crawler>
</modules>
我的配置
app/code/local/VMR/Crawler/etc/config.xml
我的配置代码
<?xml version="1.0"?>
<config>
<modules>
<VMR_Crawler>
<version>0.0.1</version>
</VMR_Crawler>
</modules>
<global>
<models>
<vmr_crawler>
<class>VMR_Crawler_Model</class>
<resourceModel>vmr_crawler_resource</resourceModel>
</vmr_crawler>
<vmr_crawler_resource>
<class>VMR_Crawler_Model_Resource</class>
</vmr_crawler_resource>
<pagecache>
<rewrite>
<crawler>VMR_Crawler_Model_Crawler</crawler>
</rewrite>
</pagecache>
<pagecache_resource_crawler>
<rewrite>
<attribute>VMR_Crawler_Model_Resource_Crawler</attribute>
</rewrite>
</pagecache_resource_crawler>
</models>
</global>
</config>
我的模型
app/code/local/VMR/Crawler/Model/Crawler.php
我的模型代码
<?php
class VMR_Crawler_Model_Crawler extends Enterprise_PageCache_Model_Crawler
{
protected function _construct()
{
$this->_init('vmr_crawler/crawler');
}
public function crawl()
{
if (!Mage::app()->useCache('full_page')) {
return $this;
}
$storesInfo = $this->getStoresInfo();
$adapter = new Varien_Http_Adapter_Curl();
foreach ($storesInfo as $info) {
$options = array(CURLOPT_USERAGENT => self::USER_AGENT);
$storeId = $info['store_id'];
$this->_visitedUrls = array();
if (!Mage::app()->getStore($storeId)->getConfig(self::XML_PATH_CRAWLER_ENABLED)) {
continue;
}
$threads = (int)Mage::app()->getStore($storeId)->getConfig(self::XML_PATH_CRAWLER_THREADS);
if (!$threads) {
$threads = 1;
}
if (!empty($info['cookie'])) {
$options[CURLOPT_COOKIE] = $info['cookie'];
}
$urls = array();
$baseUrl = $info['base_url'];
$urlsCount = $totalCount = 0;
$urlsPaths = $this->_getResource()->getUrlsPaths($storeId);
foreach ($urlsPaths as $urlPath) {
$url = $baseUrl . $urlPath;
$urlHash = md5($url);
if (isset($this->_visitedUrls[$urlHash])) {
continue;
}
$urls[] = $url;
$this->_visitedUrls[$urlHash] = true;
$urlsCount++;
$totalCount++;
if ($urlsCount == $threads) {
$adapter->multiRequest($urls, $options);
$urlsCount = 0;
$urls = array();
}
}
if (!empty($urls)) {
$adapter->multiRequest($urls, $options);
}
}
return $this;
}
}
我的资源模型
app/code/local/VMR/Crawler/Model/Resource/Crawler.php
我的资源模型代码
<?php
class VMR_Crawler_Model_Resource_Crawler extends Enterprise_PageCache_Model_Resource_Crawler
{
/**
* Retrieve URLs paths that must be visited by crawler
*
* @param $storeId
* @return array
*/
public function getUrlsPaths($storeId)
{
$bWorked = false;
$collection = array();
for($i = 0; $i < 3; $i++)
{
echo "hi";
try {
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from($this->getTable('core/url_rewrite'), array('request_path'))
->where('store_id=?', $storeId)
->where('is_system=1');
$collection = $adapter->fetchCol($select);
$bWorked = true;
}
catch(Exception $ex)
{
}
if($bWorked == true)
{
break;
}
}
return $collection;
}
}
我的资源模型mysql4类
app/code/local/VMR/Crawler/Model/Mysql4/Crawler.php
我的资源模型mysql4类代码
<?php
class VMR_Crawler_Model_Mysql4_Crawler extends Enterprise_PageCache_Model_Resource_Crawler
{
}
我的外壳课程进行测试
shell/crawler.php
我的外壳课程用于测试代码
<?php
/**
* Full Page Cache Warming Shell Script
*
* @author Jeffrey L. Roberts
**/
require_once 'abstract.php';
class Mage_Shell_Crawler extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
// echo "Starting Crawler";
$crawler = new VMR_Crawler_Model_Crawler();
$crawler->crawl();
// echo "Crawler finished";
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f crawler.php
USAGE;
}
}
$shell = new Mage_Shell_Crawler();
$shell->run();
执行外壳
cd shell
php -f crawler.php
解决方案
在黑暗中拍摄,因为您的帖子有点密集,我不确定我已经看到有人扩展了模型资源并覆盖了 _construct
两者中的方法,但是PHP抱怨的第37行是
$urlsPaths = $this->_getResource()->getUrlsPaths($storeId);
这是由您的错误消息备份的
Fatal error: Call to a member function getUrlsPaths() on a non-object in /jlr/sites/mage/app/code/local/VMR/Crawler/Model/Crawler.php on line 37
因此,Magento试图实例化模型的资源,但不能。因此,如果您正在寻找一个起点,请尝试追踪
$o = Mage::getModelResource('vmr_crawler/crawler');
并确定其为什么返回错误。
一些猜测。首先,您包括一个
<deprecatedNode>vmr_crawler_mysql4</deprecatedNode>
但是你没有 vmr_crawler_mysql4
XML文件中的节点。为什么在这里?
其次,您的爬行者资源模型似乎是用一个初始化的 core/url_rewrite
?
protected function _construct()
{
$this->_init('core/url_rewrite', 'url_rewrite_id');
}
我不确定该怎么办,但它看起来不错(然后,我还不清楚您在这里要做什么,而且目前我没有任何可用的企业版本)