Hello, I hope you can help me, I am using an external api of currency types, this information which I save in the database. However, I want to use a cron that allows to query every time the external api and update them in my database. My question is, what is the best way to empty all the data contained in a database table from the cron?
$this->curl->get(self::CURRENCY_API); $body = $this->curl->getBody(); $currency_array = json_decode($body, true); //Delete data line code... $model = $this->currencyFactory->create(); foreach ($currency_array['results'] as $key => $value) { $model->setData([ 'currency_type' => $key, 'currency_value' => $value ]); $model->save(); }
Get the magento cron schedule collection using this class \Magento\Cron\Model\ResourceModel\Schedule\Collection and add filter according to your need & truncate all the data in the table from db using the below code.
public function __construct( \Magento\Cron\Model\ResourceModel\Schedule\Collection $cron ) { $this->cron = $cron; } public function execute() { $collection = $this->cron->addFieldToFilter(['job_code'], [[ 'like' => "%moduleName%"]]); if (isset($collection) && $collection->getSize() > 0) { $collection->walk('delete'); } }
I hope it will help you