0

class Delete extends \Magento\Backend\App\Action { /** * @var \Webkul\Grid\Model\GridFactory */ var $postFactory;

/**
 * @param \Magento\Backend\App\Action\Context $context
 * @param \Webkul\Grid\Model\GridFactory $gridFactory
 */
public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Stathmos\HelloWorld\Model\PostFactory $postFactory
) 
{
    parent::__construct($context);
    $this->postFactory = $postFactory;
}

/**
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function execute()
{
    $data = $this->getRequest()->getPostValue();
    if (!$data) {
        $this->_redirect('stathmos_helloworld/post/index');
        return;
    }
    try {              

        $rowData = $this->postFactory->create();
              //  $rowData->addData($data)->save();

        $rowData->setData($data);
        // if (isset($data['id'])) {
        //     $rowData->setEntityId($data['id']);
        // }
        $rowData->delete();
        $this->messageManager->addSuccess(__('Row data has been successfully deleted.'));
    } catch (\Exception $e) {
        $this->messageManager->addError(__($e->getMessage()));
    }
    $this->_redirect('stathmos_helloworld/post/index');
}

/**
 * @return bool
 */
protected function _isAllowed()
{
    return $this->_authorization->isAllowed('Stathmos_HelloWorld::delete');
}

}

namespace Stathmos\HelloWorld\Ui\Component\Listing\Post\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponentFactory; use Magento\Ui\Component\Listing\Columns\Column; use Magento\Framework\UrlInterface;

class Action extends Column {

/** Url path */
const URL_PATH_EDIT = 'helloworld/post/edit';
const URL_PATH_DELETE = 'helloworld/post/delete';
const URL_PATH_VIEW = 'helloworld/post/view';

protected $actionUrlBuilder;
protected $urlBuilder;

public function __construct(
    ContextInterface $context, 
    UiComponentFactory $uiComponentFactory, 
    UrlInterface $urlBuilder, 
    array $components = [], 
    array $data = []
) {
    $this->urlBuilder = $urlBuilder;
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function prepareDataSource(array $dataSource) {
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as & $item) {
            $name = $this->getData('name');
            if (isset($item['post_id'])) {
                $item[$name]['edit'] = [
                    'href' => $this->urlBuilder->getUrl(
                            self::URL_PATH_EDIT, [
                                'id' => $item['post_id']
                            ]
                    ),
                    'label' => __('Edit')
                ];
                $item[$name]['delete'] = [
                    'href' => $this->urlBuilder->getUrl(
                            self::URL_PATH_DELETE, [
                                'id' => $item['post_id']
                            ]
                    ),
                    'label' => __('Delete'),
                    'confirm' => [
                        'title' => __('Delete "${ $.$data.title }"'),
                        'message' => __('Are you sure you wan\'t to delete a "${ $.$data.title }" record?')
                    ]
                ];
                $item[$name]['preview'] = [
                    'href' => $this->urlBuilder->getUrl(
                            self::URL_PATH_VIEW, [
                                'id' => $item['post_id']
                            ]
                    ),
                    'label' => __('View')
                ];
            }
        }
    }

    return $dataSource;
}

}

0

Try the following way:


$rowData = $this->postFactory->create();
$rowData->load($id) // id is the deleted post id
$rowData->delete();

Here is an example

You should use the repository for CRUD operation.

4
  • Invalid security or form key. Please refresh the page. it is giving this and redirecting to the dashboard
    – kks
    Jun 15 at 9:26
  • Invalid security or form key. Please refresh the page. its not deleting
    – kks
    Jun 15 at 9:36
  • Don't post data. Check the simple CMS module from magento. Your key related error leads to your security key is missing or wrong.
    – Sohel Rana
    Jun 15 at 10:57
  • I found it error was it was not getting the right url for the controller. Please suggest me for the edit module how I can edit the row?
    – kks
    Jun 15 at 11:06

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.