1

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.

asked May 8, 2017 at 3:26
8
  • Replace class HelloWorld extends AbstractDb with class HelloWorld extends \Magento\Framework\Model\AbstractModel Commented May 8, 2017 at 3:34
  • @AnkitShah replacing the one in Model/ResourceModel/HelloWorld.php? Is that the same as the one inherited in Model file? Commented May 8, 2017 at 3:41
  • Sorry ignore it. Please follow mageplaza.com/magento-2-module-development Commented May 8, 2017 at 4:03
  • Did you try after delete var/generation/* and clear cache? Commented May 8, 2017 at 4:43
  • @AnkitShah I tried creating model interface and implementing abstract methods based on the link you provided, but it still give me the same error as stated in my question. Commented May 8, 2017 at 4:47

1 Answer 1

1

You have typo in your app/code/Ciptaloka/HelloWorld/Model/HelloWorld.php at line 3:

namespace Ciptloka\HelloWorld\Model;

It should be:

namespace Ciptaloka\HelloWorld\Model;

Notice the difference between Ciptloka and Ciptaloka.

answered May 8, 2017 at 6:38
1
  • Wow... how could my eyes miss that?! Thanks for spotting it! Everything works flawlessly now. Commented May 8, 2017 at 7:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.