1

I have a little problem. I am currently working on an synchronize job for cron.

The sync function just gets the newest elements from an api and updates in the Magento db. The Magento version is: 2.3.1 .

Here is the function:

 public function synchronize()
{
    $writer = new Stream(BP . '/var/log/cron.log');
    $logger = new Logger();
    $logger->addWriter($writer);

    $login = $this->login();

    $dateTimestamp = $this->getLatestDate('/tmp/marketsSync.txt');

    $items = $this->getNewestItems($dateTimestamp, $login);

    foreach ($items as $item) {
        $srcItem = $this->_marketFactory->create();
        $srcItem->setContent($item);

        $marketCollection = $this->_marketCollectionFactory->create();
        $marketCollection
            ->addFieldToSelect('*')
            ->addFieldToFilter('id_location', ['eq' => $srcItem->getData('id_location')])
            ->load();

        $logger->info('found? :'. $marketCollection->getSize());

        if ($marketCollection->getSize()) {
            $tgtItem = $marketCollection->getFirstItem();

            $srcTime = strtotime($srcItem->getData('date_upd'));
            $tgtTime = strtotime($tgtItem->getData('date_upd'));

            if ($srcTime > $tgtTime) {
                $this->updateElement($tgtItem, $srcItem);
            }
        } else {
            $logger->info('create');
            $this->createElement($srcItem, $marketCollection);
        }
    }

    $this->setLatestDate('/tmp/marketsSync.txt', date("Y-m-d H:i:s") );
}

public function createElement(Synchronizable $obj)
{
    $item = $this->_marketFactory->create();
    $item->setContent($obj->getData());
    $item = $item->save();
}

public function updateElement(Synchronizable $tgtObj, Synchronizable $srcObj)
{
    $writer = new Stream(BP . '/var/log/cron.log');
    $logger = new Logger();
    $logger->addWriter($writer);

    $tgtObj->setContent($srcObj->getData());

    $logger->info($tgtObj->getData());

    $tgtObj->save();
}

I've got the Object to update from the marketCollection via getFirstItem(). Now in the updateElement() function i use the the api data to update the object with

$tgtObj->setContent($srcObj->getData());

This function is just a foreach loop:

 public function setContent($content)
{
    foreach ($content AS $key => $value){
        $this->setData($key, $value);
    } 
}

I know that this method is already deprecated, but why does save allways create a new entry? (it creates the same data but with a new id)

  • At ` $this->updateElement($tgtItem, $srcItem);` , $srcItem is an single field valuepublic function updateElement(Synchronizable $tgtObj, Synchronizable $srcObj) but here object – Amit Bera May 29 at 10:31
0

I found out what the problem was. I had overridden getIdentities but the name was not right.

In the Model now:

 /**
 * Get ID
 *
 * @return int|null
 */
public function getId()
{
    return $this->getData('location_id');
}

/**
 * Return identities
 * @return string[]
 */
public function getIdentities()
{
    return [self::CACHE_TAG . '_' . $this->getId()];
}

And now it works.

Your Answer

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

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