0

I have one cronjob needs to update status periodically.

Target table name is "Custom"

if custom table contain customid, user_id, status, exp_date columns.

I have scheduler php file call "custom.php".

I added below in __construct(

use Test\Subscription\Model\ResourceModel\Custom\CollectionFactory

public function __construct(
     CollectionFactory $collectionFactory
) {
     $this->collectionFactory=$collectionFactory;
}

public function execute() {
    $custom_obj = $this->collectionFactory->create();

    $custom_obj->addFieldToSelect('*')->load();
    $items = $custom_obj->getItems();

    foreach($items as $key=>$item){
         if( date("Y-m-d") > $item->getDataByKey("exp_date") ) {
              // save column
         }
    }
}

In this case does anyone know how to update column for this case ( to fill save column )? Also, Does anyone know best practice for this kind of case for update?

Thank you!

1 Answer 1

0

Try something like:


public function execute() 
{
    $collection = $this->collectionFactory->create();

    foreach ($collection as $item) {
         if ( date("Y-m-d") > $item->getData("exp_date") ) {
              $item->setCustomColumn('Data')->save();
         }
    }
}

If your column name is status, then

$item->setStatus('inActive')->save();
2
  • Hi! Thank you for answer it. you meant if I want to change "Status" column then $item->setCustomStatus('inActive')->save() ?
    – clear.choi
    Commented Aug 30, 2020 at 17:00
  • Check updated answer. Should be $item->setStatus('inActive')->save();.
    – Sohel Rana
    Commented Aug 31, 2020 at 3:37

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.