Question

I have created a model class as like below.

Vendor\Module\Model/Bulider.php

namespace Vendor\Module\Model;

use Magento\Framework\Exception\QualificationException;
 class Builder extends \Magento\Framework\Model\AbstractModel
{

   public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    array $data = []
) {
    parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}

/**
 * @return void
 */
public function _construct()
{
    $this->_init('Vendor\Module\Model\ResourceModel\Builder');
 }

}

then Vendor\Module\Model\ResourceModel\Builder.php

 namespace Vendor\Module\Model\ResourceModel;

 class Builder extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

protected $_builderFactory;

public function __construct(
    \Magento\Framework\Model\ResourceModel\Db\Context $context,
    \Vendor\Module\Model\BuilderFactory $_builderFactory
)
{
    $this->_builderFactory = $_builderFactory;
    parent::__construct($context);
}


public function _construct()
{
    $this->_init('builder_table', 'id');
}

public function builder($pid,$type,$id,$date) {
    $log = $this->_builderFactory->create();
    $log->setPId($pid);
    $log->setProductType($type);
    $log->setUnitId($id);
    $log->setCreatedAt($date);
    $log->save();
  }
}

I have injected this class in my controller and inserting data as like below.

class Save extends \Magento\Backend\App\Action
{

 protected $_builderResourceModel;
 protected $_storeManager;

 public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Vendor\Module\Model\ResourceModel\Builder $builderResourceModel
 ) {
    $this->_storeManager     = $storeManager;
    $this->_builderResourceModel = $builderResourceModel;
    parent::__construct($context);
 }

 public function execute()
 {
   $result = array();// this is the model data
   $modelId = $result['id'];
   $date = date('Y-m-d H:i:s');
   foreach($result['core_units'] as $unit){
      $type = "C";
      $this->_builderResourceModel->builder($modelId,$type,$unit,$date);
   }

    foreach($data['optional_units'] as $option){
       $type = "O";
      $this->_builderResourceModel->builder($modelId,$type,$option,$date);
    }

   }
}

the current data structure is like below

enter image description here

A sample result data is given here

  Array
   (
     [id] => 2    
     [core_units] => Array
     (
        [0] => 31
        [1] => 63
     )
      [optional_units] => Array
    (
        [0] => 31
        [1] => 63
        [2] => 82
       )
     )

so, while inserting data i need to check for the duplicate data insert.

Before inserting i need to check if same data exist or not in the table, if data exist with same i need to update otherwise i need to insert new row.

Can this be achieved any other way. Please someone look into it and update me your ideas. Thanks

Was it helpful?

Solution

Modify builder method following way:

public function builder($pid,$type,$id,$date)
{
    $log = $this->_builderFactory->create();
    $log = $log->load($pid, 'p_id');
    if (!$log->getId()) {
        $log = $this->_builderFactory->create();
    }
    $log->setPId($pid);
    $log->setProductType($type);
    $log->setUnitId($id);
    $log->setCreatedAt($date);
    $log->save();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top