1

I am trying to insert data into my custom table using my Model class.

Using the InstallSchema only table got created, but values are not getting added.

(only main code added)

etc/di

<preference for="Vendor\Module\Api\Data\QueueInterface" type="Vendor\Module\Model\Queue" />

Vendor\Module\etc\webapi.xml

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/sync/getProduct" method="GET">                                    <!--route: contains the URL and a method (GET, POST, and DELETE) -->
    <service class="Vendor\Module\Api\Data\QueueInterface" method="getProductUpdate"/>   <!--service: defines the interface class and the method for handling -->
    <resources>
        <resource ref="anonymous" />
    </resources>
</route>

Vendor\Module\Api\Data\QueueInterface.php

namespace Vendor\Module\Api\Data;
interface QueueInterface
{
    public function getProductUpdate($limit);
}

Vendor\Module\Model\Queue.php

 class Queue extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface, 
                                                                  \Vendor\Module\Api\Data\QueueInterface
 {
   const STATUS_ENABLED = 1;
   const STATUS_DISABLED = 0;
   const CACHE_TAG = 'tgs_queue';  //custom table name
   protected $_cacheTag = 'tgs_queue';
   protected $_eventPrefix = 'tgs_queue';

  public function getProductUpdate($limit)
  {
     ...
  }

Vendor\Module\Model\ResourceModel\Queue.php

class Queue extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected function _construct()
{
    $this->_init('tgs_queue','id');
  }
}

Vendor\Module\Model\ResourceModel\Queue\Collection.php

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
  public function _construct()
  {
    $this->_init('Vendor\Module\Model\Queue','Vendor\Module\Model\ResourceModel\Queue');
  }
}

etc/events.xml

<event name="catalog_product_save_after">
    <observer name="catalog_product_save_after_handler" instance="Vendor\Module\Observer\SyncObserver"/>
</event>

Vendor\Module\Observer\SyncObserver.php

use \Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class SyncObserver implements ObserverInterface
{
  function __construct(
    \Vendor\Module\Model\QueueFactory $syncQueueFactory      
)
{
    $this->syncQueueFactory = $syncQueueFactory;
    //$this->_queue = $this->syncQueueFactory->create();
}

  public function execute(\Magento\Framework\Event\Observer $observer)
  {
    $event = $observer->getEvent();
    $this->syncQueueFactory->productUpdate();
  }
}     

UPDATED Vendor\Module\Model\QueueFactory.php

use Vendor\Module\Model\QueueFactory as QueueAlias;
class QueueFactory extends \Magento\Framework\Model\AbstractModel
{
    protected $syncQueueFactory;

    public function __construct(
     \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\View\LayoutInterface $layout,
    \Magento\Framework\View\LayoutFactory $layoutFactory,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\App\Request\Http $request,
    \Magento\Catalog\Model\Session $catalogSession, 
    \Psr\Log\LoggerInterface $logger,   
    QueueAlias $syncQueueFactory,    //If I keep this i get circular dependency error.
    array $data = []
    ) {
     $this->syncQueueFactory = $syncQueueFactory;
    $this->logger = $logger;
    $this->_queue = $this->syncQueueFactory->create();
parent::__construct(
        $context,
         $registry,
        //$resource,
        //$resourceCollection, 
        $data
    );
   }

public function productUpdate(\Magento\Framework\Event\Observer $observer)
{...}

1) Have i used the methods and events correctly? Are my folders structures proper?

2) Why is my data not getting inserted into my custom table?

3) The purpose of this is im trying to add the product id using my custom api!

Suggestion highly appreciated!

1 Answer 1

0

productUpdate should not be in model class, move it to separate

1
  • Is my QueueFactory required? if not where to keep my productUpdate()?
    – Sushivam
    Jul 22, 2016 at 9:54

Your Answer

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

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