1

I created a new table company_shipping_quote.

I am writing data into it like this:

$quoteId = $quote->getId();
$html = $this->_generateNote($expressRule);

$quoteModel = $this->_companyShippingQuoteFactory->create();
$quoteModel->setQuoteId($quote->getId());
$quoteModel->setExpressNote($html);
$quoteModel->save();

But this does not check if there is already an entry with the value for quote_id and it just creates duplicates.

enter image description here

How can I check if there is already a row with that quote_id before creating it?

0

4 Answers 4

2

try below code - Another way

<?php

protected $_companyShippingQuoteFactory;

public function __construct(
    ...
    \Company\Shipping\Model\QuoteFactory $companyShippingQuoteFactory
) {
    ...
    $this->_companyShippingQuoteFactory = $companyShippingQuoteFactory;
}

...

$quote_id =13718;

$quoteModel = $this->_companyShippingQuoteFactory->create();
$quoteModel->load($quote_id, 'quote_id');
if ($quoteModel->getId()) {
    echo "Updated";
} else {
   echo "New Added";
}
$quoteModel->save();
6
  • check it's different
    – Devidas
    Oct 21, 2020 at 11:20
  • my code is execute fast
    – Devidas
    Oct 21, 2020 at 11:21
  • change factory and check I have update code check. i know it's working
    – Devidas
    Oct 21, 2020 at 11:42
  • do not use CollectionFactory use your model factory like QuoteFactory because our code does not load all collection. it's load single only so that why it executes fast
    – Devidas
    Oct 21, 2020 at 11:47
  • I accept your solution, because it looks more performant and I don't need to use another class. Thx!
    – Black
    Oct 21, 2020 at 11:49
2

Try This Code

protected $_companyShippingQuoteFactory;

public function __construct(
    ...
    \Company\Shipping\Model\Quote\ResourceModel\CollectionFactory $companyShippingQuoteFactory
) {
    ...
    $this->_companyShippingQuoteFactory = $companyShippingQuoteFactory;
}

...

$quote_id =13718;

$quoteModel = $this->_companyShippingQuoteFactory->create();
$quoteModel->addFieldToFilter('quote_id',$quote_id)

if(count($quoteModel->getData())){
    echo "quote id added";
} else {
    echo "New quote id";
}
2
  • I had to use \Company\Shipping\Model\ResourceModel\Quote\CollectionFactory $companyShippingQuoteFactory. I generated my model with mage2gen.com
    – Black
    Oct 21, 2020 at 11:26
  • The path seems to be wrong. If I generate the models from mage2gen, then it does not create a file under the path
    – Black
    Oct 21, 2020 at 12:21
2

There might be a more straight forward solution.

If quote_id is unique in your database it makes more sense to use it as a primary key.

So you remove entity_id from db_schema.xml and change the resourceModel accordingly changing _idFieldName to quote_id. You then add this to the _construct(): (check this question how to save data into custom table):

$this->_isPkAutoIncrement = false;

Of your resource model, you then be able to change the value by code like this assuming the quoteId already exists in the database:

$quoteModel = $this->_companyShippingQuoteFactory->create();
$quoteModel->setQuoteId($quote->getId());
$quoteModel->setExpressNote($newHTMLhtml);
$quoteModel->save();
3
  • But the quote_id has to be a foreign key of the key entity_id in the table quote. I can't define it as Primary and Foreign Key at the same time.
    – Black
    Oct 21, 2020 at 12:19
  • I think it is no problem to have something as both a foreign key and a primary key, did not test in Magento though, but in mysql I think it is legal
    – Kristoffer
    Oct 21, 2020 at 12:30
  • I tested it in magento, but it just creates the PRIMARY key.
    – Black
    Oct 21, 2020 at 12:38
0

since using model is deprecated we use this to load with specific field and save data.

we need these 4 classes to load and update or add new data

1. `\vendor\module\Model\testFactory $ModeltestFactor`
2. `\vendor\module\Model\ResourceModel\test $ResourceModelytest
3.`\vendor\module\Model\testRepository $ModelRepositorytest`
4.`\vendor\module\Api\Data\testInterfaceFactory $InterfaceFactorytest`

and in class

  $model = $this->ModeltestFactor->create();
  $this->ResourceModeltest->load($model,'123456','column_name');

  if ($model->getData()){
      $model->setBinTo('Bin_Code');
  }else {
      $model = $this->InterfaceFactorytest->create();
      $model->setNumber('No');
      $model->setMoveBy('jjjjj');
      $model->setItem('Item_No');
  }

  $this->ModelRepositorytest->save($model);

EDIT or you can use only two classes

1. `\vendor\module\Model\testFactory $ModeltestFactor`
2. `\vendor\module\Model\ResourceModel\test $ResourceModelytest

and in class

  $model = $this->ModeltestFactor->create();
  $this->ResourceModeltest->load($model,'123456','column_name');

  if ($model->getData()){
      $model->setBinTo('Bin_Code');
  }else {
      $model->setNumber('No');
      $model->setMoveBy('jjjjj');
      $model->setItem('Item_No');
  }

  $this->ModelRepositorytest->save($model);

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.