2

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

1 Answer 1

3

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();
}
8
  • hi @Sohel,this code will update the existing record? Commented May 28, 2019 at 9:22
  • yes............
    – Sohel Rana
    Commented May 28, 2019 at 9:23
  • I need to insert new row only if not found the record with same values for all field, right now you are checking with only p_id, Commented May 28, 2019 at 9:28
  • The above code filters with p_id and updates all records, regardless of checking whether all other columns are same or not Commented May 28, 2019 at 9:29
  • Delete all record first, then try
    – Sohel Rana
    Commented May 28, 2019 at 9:31

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.