0

I am trying to create custom table in a plugin and trying to write model classes for it. Custom database tables is successfully generated. However I am struggling to let magento auto generate model factory classes.

When bin/magento setup:di:compile command is executed only Interceptor.php file is generated and not Factory class.

Model interface

<?php
namespace Company\OrderListener\Api\Data;

interface CustomOrderModelInterface {

    const ID           = 'id';
    const ORDERID      = 'orderId';
    const PROCESSED    = 'processed';

    //Getter & Setter interface definitions
}

Model class

<?php
namespace Company\OrderListener\Model;

use Company\OrderListener\Api\Data\CustomOrderModelInterface;
use \Magento\Framework\Model\AbstractModel;
use \Magento\Framework\DataObject\IdentityInterface;

/**
 * Undocumented class
 */
class CustomOrderModel extends \Magento\Framework\Model\AbstractModel implements CustomOrderModelInterface , IdentityInterface {
    const CACHE_TAG = 'orderlistener_custom_order_model';

    /**
     * Model cache tag for clear cache in after save and after delete
     *
     * @var string
     */
    protected $_cacheTag = self::CACHE_TAG;

    /**
     * Prefix of model events names
     *
     * @var string
     */
    protected $_eventPrefix = 'custom_order_model';

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Company\OrderListener\Model\ResourceModel\CustomOrderModel');
    }

    //Getters and Setters
}

Resource Model class

<?php
namespace Company\OrderListener\Model\ResourceModel;

class CustomOrderModel extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb {
    
    /**
     * Initialize resource model
     *
     * @return void
     */
    //custom_order is custom table and id is primary key 
    protected function _construct()
    {
        $this->_init("custom_order","id");
    }

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

Collection class

<?php
namespace Company\OrderListener\Model\ResourceModel\CustomOrderModel;

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

    public function __construct() {
        $this->_init('Company\OrderListener\Model\CustomOrderModel',
        'Company\OrderListener\Model\ResourceModel\CustomOrderModel');
    }
}

Most of the guides and magento documents suggest to create these files and use auto generated factory class to persist/access data to/from custom database tables in plugin. However factory class are not auto generated with both bin/magento setup:di:compile and bin/magento setup:upgrade commands.

Does some definitions needs to be done in di.xml for this to work?

EDIT: I have added preference for interface in di.xml still no luck

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Company\OrderListener\Api\Data\CustomOrderModelInterface" type="Company\OrderListener\Model\CustomOrderModel" />
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.