1

Using magento 2.2, CRUD, API. I got an error while try to access method getCollection().

{ "messages": { "error": [ { "code": 500, "message": "Fatal Error: 'Cannot declare class Iota\EShopping\Model\ResourceMode\Social\Collection, because the name is already in use' in '/home/esh_www/app/code/Iota/EShopping/Model/ResourceModel/Social/Collection.php' on line 20", "trace": "Trace is not available." } ] } }

CALL

use Magento\Customer\Model\CustomerFactory;
use Iota\EShopping\Model\SocialFactory;
use Iota\EShopping\Setup\SchSocialLogin;
use Iota\EShopping\Api\Data\SocialLoginModel as Login;

/**
 *
 * @author channarith.bong
 *        
 */
class CustomerManagement implements \Iota\EShopping\Api\CustomerManagementInterface
{

    /**
     * 
     * @param SocialFactory $socialFactory
     * @param CustomerFactory $customerFactory
     */
    public function __construct(
        SocialFactory $socialFactory,
        CustomerFactory $customerFactory
    ){
        $this->customerFactory = $customerFactory;
        $this->socialFactory = $socialFactory;
    }


    /**
     * 
     * @return string
     */
    public function validateCustomerLogin()
    {
        $socialCollection = $this->socialFactory->create()->getCollection();
    }
}

Model

<?php
namespace Iota\EShopping\Model;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;


/**
 * 
 * @author channarith.bong
 *
 */
class Social extends AbstractModel
{
    /**
     * 
     * @param Context $context
     * @param Registry $registry
     * @param AbstractResource $resource
     * @param AbstractDb $resourceCollection
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        AbstractResource $resource = null,
        AbstractDb $resourceCollection = null,
        array $data = []
    ){
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    }

    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('Iota\EShopping\Model\ResourceModel\Social');
    }
}

Collection

<?php
namespace Iota\EShopping\Model\ResourceMode\Social;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

/**
 * 
 * @author channarith.bong
 *
 */
class Collection extends AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init('Iota\EShopping\Model\Social', 'Iota\EShopping\Model\ResourceModel\Social');
    }
}

Resource

<?php
namespace Iota\EShopping\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
/**
 * 
 * @author channarith.bong
 *
 */
class Social extends AbstractDb
{
    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('iot_td_social_login', 'id');
    }
}
Bong Channarith
  • 780
  • 11
  • 31
  • Check namespace of collection file – Prince Patel Aug 09 '18 at 05:14
  • Check module files and folder name also try after the commands php bin/magento cache:flush and php bin/magento setup:di:compile – kunj Aug 09 '18 at 05:20
  • In Mode Developer, I used php bin/magento cache:flush and php bin/magento setup:di:compile – Bong Channarith Aug 09 '18 at 06:22
  • Have you corrected namespace Iota\EShopping\Model\ResourceMode\Social; To namespace Iota\EShopping\Model\ResourceModel\Social; in your Collection.php and same for folder name From ResourceMode To ResourceModel ? – kunj Aug 09 '18 at 08:49
  • I'm created Magento 2 tutorials channel please Like and Support https://www.youtube.com/channel/UC2JghzAPB7wNBDY0u-OtAig/videos – Sonu Madeshiya Oct 07 '22 at 05:41

1 Answers1

1

You did the Mistake;

See your __constact function

 public function __construct(
        SocialFactory $socialFactory,
        CustomerFactory $customerFactory
    ){
        $this->customerFactory = $customerFactory;
        $this->socialFactory = $socialFactory;
    }

It means $this->socialLoginFactory undefine variable. You have to use $this->socialFactory as it is object of Iota\EShopping\Model\SocialFactory.

Seems some issue in model class

Replace model class by below code:

MOdel Class

namespace Iota\EShopping\Model;

use Magento\Framework\Model\AbstractModel;

class Social extends AbstractModel
{
    protected function  _construct()
    {
       $this->_init(\Iota\EShopping\Model\ResourceModel\Social::class);
    }

}

Resource MOdel Class

namespace Iota\EShopping\Model\ResourceModel;
class Social extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context
    )
    {
        parent::__construct($context);
    }

    protected function _construct()
    {
        $this->_init('iot_td_social_login', 'id');
    }

}

Collection Classs

namespace Iota\EShopping\Model\ResourceModel\Social;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init(\Iota\EShopping\Model\Social::class,
            \Iota\EShopping\Model\ResourceModel\Social::class);
    }
}

After that, you have to do di compile

Important:

getCollection is deprecated function On Iota\EShopping\Model\Social.

SO, suggesting you use Collection Class directly or Implement Server contact interface

Amit Bera
  • 77,456
  • 20
  • 123
  • 237