1

I'm trying to use an unset magic method to delete a row from the table.

Following is the line of code from execute method of my CLI command.

$item->unsEquipmentName($input->getArgument(self::INPUT_KEY_NAME));

What do I need to add more to get this command working.

The command is registered properly when I check it in the command list and it executes on bash with no error however nothing changes in the database.

How should I close my execute method?

1

I'm not sure what $item is, but I can explain what the methods that start with uns do.
If your $item is an instance of a class that inherits Magento\Framework\DataObject and you have no method defined called unsEquipmentName calling $item->unsEquipmentName() is equivalent to $item->unsetData('equipment_name').
This does not affect you database.
It just unsets $_data['equipment_name'] from your object
Also, this method does not take any parameters.
you will need to persist your item after unsetting a value.
You can do that with either $item->save() which is not recommended or you can use the repository class for your entity, if you have one.
Maybe you should explan better what you are trying to achieve.

  • I was trying to delete all entries of "Equipment 1" from a equipment_name column. I have figured it out and following is the solution. – FarhanS Mar 1 at 4:03
0
$itemName = $input->getArgument(self::INPUT_KEY_NAME);
        $itemCollection = $this->collectionFactory->create()->getItemsByColumnValue('equipment_name',$itemName); //array type
        $equipment = $this->itemFactory->create();
        $output->writeln('<info>Collection items are ' . var_dump($itemCollection) . '</info>');
        foreach($itemCollection as $key=>$equipment)
        {
            $equipment->load($equipment['id']);
            $equipment->delete();
        }

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.