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');
    }
}
5
  • Check namespace of collection file Commented Aug 9, 2018 at 5: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
    Commented Aug 9, 2018 at 5:20
  • In Mode Developer, I used php bin/magento cache:flush and php bin/magento setup:di:compile Commented Aug 9, 2018 at 6: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
    Commented Aug 9, 2018 at 8:49
  • I'm created Magento 2 tutorials channel please Like and Support youtube.com/channel/UC2JghzAPB7wNBDY0u-OtAig/videos Commented Oct 7, 2022 at 5:41

1 Answer 1

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

7
  • I update as you suggested, but I still got that error. how i need to do? Commented Aug 9, 2018 at 6:16
  • share the code of \Iota\EShopping\Api\CustomerManagementInterface
    – Amit Bera
    Commented Aug 9, 2018 at 6:21
  • You have to implement service contract to your custom module in Magento properly. Seems you have the issue on that. magento.stackexchange.com/questions/115269/…
    – Amit Bera
    Commented Aug 9, 2018 at 6:22
  • Not yet, How to ? Commented Aug 9, 2018 at 6:24
  • 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
    – Amit Bera
    Commented Aug 9, 2018 at 6:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.