When I try to load collection via getCollection
method in my custom Block, I get this error in apache2 log.
The complete error is:
[Mon May 08 09:53:53.485661 2017] [proxy_fcgi:error] [pid 1996:tid 140084720269056] [client 127.0.0.1:50323] AH01071: Got error 'PHP message:
PHP Fatal error: Uncaught Error: Class 'Ciptaloka\\HelloWorld\\Model\\HelloWorld' not found in /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:93
Stack trace:
#0 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/ObjectManager/Factory/Compiled.php(88): Magento\\Framework\\ObjectManager\\Factory\\AbstractFactory->createObject('Ciptaloka\\Hello...', Array)
#1 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/ObjectManager/ObjectManager.php(57): Magento\\Framework\\ObjectManager\\Factory\\Compiled->create('Ciptaloka\\Hello...', Array)
#2 /opt/bitnami/apps/magento/htdocs/var/generation/Ciptaloka/HelloWorld/Model/HelloWorldFactory.php(43): Magento\\Framework\\ObjectManager\\ObjectManager->create('Ciptaloka\\Hello...', Array)
#3 /opt/bitnami/apps/magento/htdocs/app/code/Ciptaloka/HelloWorld/Block/Catalog/Product/HelloWorld.php(75): Ciptaloka\\HelloWorld\\Model\\HelloWorldFactory->create()
#4 [internal function]: Ciptaloka\\HelloWorld\\Block\\Catalog\\P...
', referer: http://192.168.1.171/
This is the directory structure of my custom module:
app/code/Ciptaloka/HelloWorld/ ├── Block │ └── Catalog │ └── Product │ └── HelloWorld.php ├── etc │ ├── adminhtml │ │ ├── routes.xml │ │ └── system.xml │ ├── config.xml │ └── module.xml ├── Helper │ └── ConfigurationHelper.php ├── Model │ ├── HelloWorld.php │ ├── ResourceModel │ │ ├── HelloWorld │ │ │ └── Collection.php │ │ └── HelloWorld.php │ └── Source │ └── TextAlign.php ├── registration.php ├── Setup │ └── InstallSchema.php └── view └── frontend ├── layout │ └── catalog_product_view.xml ├── templates │ └── product │ └── hello.phtml └── web └── css └── hello.css
As you can see in the dir tree, Ciptaloka\HelloWorld\Model\HelloWorld does exist. And this is the file content of it:
<?php
namespace Ciptloka\HelloWorld\Model;
use \Magento\Framework\Model\AbstractModel;
class HelloWorld extends AbstractModel
{
/**
* Model constructor
*/
protected function _construct()
{
$this->_init('Ciptaloka\HelloWorld\Model\ResourceModel\HelloWorld');
}
}
And the other files;
Model/ResourceModel/HelloWorld.php (The table ciptaloka_helloworld
is defined.):
<?php
namespace Ciptaloka\HelloWorld\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class HelloWorld extends AbstractDb
{
/**
* Model initialization
*/
protected function _construct()
{
$this->_init('ciptaloka_helloworld', 'id');
}
}
Model/ResourceModel/HelloWorld/Collection.php:
<?php
namespace Ciptaloka\HelloWorld\Model\ResourceModel\HelloWorld;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
/**
* Collection constructor
*/
protected function _construct()
{
$this->_init(
'Ciptaloka\HelloWorld\Model\HelloWorld',
'Ciptaloka\HelloWorld\Model\ResourceModel\HelloWorld'
);
}
}
the generated factory var/generation/Ciptaloka/HelloWorld/Model/HelloWorldFactory.php:
<?php
namespace Ciptaloka\HelloWorld\Model;
class HelloWorldFactory
{
protected $_objectManager = null;
protected $_instanceName = null;
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = 'Ciptaloka\\HelloWorld\\Model\\HelloWorld')
{
$this->_objectManager = $objectManager;
$this->_instanceName = $instanceName;
}
public function create(array $data = array())
{
return $this->_objectManager->create($this->_instanceName, $data);
}
}
and, the Block file:
<?php
namespace Ciptaloka\HelloWorld\Block\Catalog\Product;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \Ciptaloka\HelloWorld\Helper\ConfigurationHelper;
use \Ciptaloka\HelloWorld\Model\HelloWorldFactory;
class HelloWorld extends Template
{
/**
* @var ConfigurationHelper
*/
protected $_helper;
/**
* @var HelloWorldFactory
*/
protected $_helloWorldFactory;
public function __construct(
Context $context,
HelloWorldFactory $helloWorldFactory,
array $data = [],
ConfigurationHelper $helper
) {
parent::__construct($context, $data);
$this->_helloWorldFactory = $helloWorldFactory;
$this->_helper = $helper;
}
/**
* Returns 'Hello World!' string
*
* @return \Magento\Framework\Phrase
*/
public function getHelloWorldTxt()
{
return __('Hello World!');
}
public function getBlockLabel()
{
return $this->_helper->getBlockLabel();
}
public function getTextAlign()
{
return $this->_helper->getTextAlign();
}
protected function _toHtml()
{
if ($this->_helper->getEnable()) {
return parent::_toHtml();
}
else {
return '';
}
}
public function getCollection()
{
return $this->_helloWorldFactory->create()->getCollection();
}
}
Do I make any mistake here that caused that error? Any help would be appreciated.
class HelloWorld extends AbstractDb
withclass HelloWorld extends \Magento\Framework\Model\AbstractModel
Model/ResourceModel/HelloWorld.php
? Is that the same as the one inherited in Model file?