i'm doing "Banner Slider" and looking for how to delete column in banner_slide
. Its hard to explain. So just look in my example:
So this is what i've done til now. A banner which contain 3 slide.
What i want is when i click in that checkbox to turn off the choose, it will count and render through HTML and then when i click Save Banner
or Save and Continue Edit
, it will delete columns in banner_slide
table in Mysql that have checkbox turning off.
So i'm following the catalog_category_products
in magento 2. And what they do is look like this:
Like you can see, it render into HTML like i said.
And when i click on Save Banner
button, it will run into Controller/../Save.php
and save data to banner_slide
table in Mysql.
So here is the Save.php:
C:\xampp\htdocs\magento\app\code\Aht\BannerSlider\Controller\Adminhtml\Manage\Save.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 20/07/2018
* Time: 4:15 CH
*/
namespace Aht\BannerSlider\Controller\Adminhtml\Manage;
use Magento\Backend\App\Action\Context;
use Aht\BannerSlider\Model\BannerFactory;
use Aht\BannerSlider\Model\BannerSlideFactory;
class Save extends \Magento\Framework\App\Action\Action
{
protected $bannerFactory;
protected $bannerSlideFactory;
public function __construct(
Context $context,
BannerFactory $bannerFactory,
BannerSlideFactory $bannerSlideFactory
) {
$this->bannerFactory = $bannerFactory;
$this->bannerSlideFactory = $bannerSlideFactory;
parent::__construct($context);
}
/**
* Save action
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$data = $this->getRequest()->getPostValue();
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
//
if ($data) {
try{
$id = $this->getRequest()->getParam('id');
$banner = $this->bannerFactory->create();
if(isset($data['banner_slide'])){
// lấy chuỗi json banner_slide được truyền vào từ form HTML.
$slide = json_decode($data['banner_slide'], true);
foreach($slide as $key => $value){
$bannerSlide = $this->bannerSlideFactory->create();
// check xem banner_slider này đã tồn tại hay chưa, nếu tồn tại thì không insert vào db.
$check = $bannerSlide->getCollection()
->addFieldToFilter('banner_id',$id)
->addFieldToFilter('slide_id',$key)
->getSize();
if($check = 0){
$bannerSlide->setBannerId($id);
$bannerSlide->setSlideId($key);
$bannerSlide->setPosition($value);
$bannerSlide->save();
}
}
}
if($id){
$banner->load($id);
$data['id'] = $id;
}
$banner->setData($data);
$banner->save();
if ($id) {
$this->messageManager->addSuccessMessage(__('Update Banner Successfully.'));
} else {
$this->messageManager->addSuccessMessage(__('Add Banner Successfully.'));
}
// check for Save and Continue Edit Button
if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/detail', ['id' => $banner->getId(), '_current' => true]);
} else {
$resultRedirect->setPath('*/*/');
}
return $resultRedirect;
}
catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
}
}
Here is my banner_slide table:
I can save into the database through foreach loop successfully the banner_slide
which "checkbox" turning on, but i dont know how to do that with banner_slide
which turning off. So i need an idea on how to do that...
Thanks for reading. Have a good day :)
EDIT 1:
So i've found 2 way to do this:
- Delete all the record in table and then insert again.
- Select all the record in table and compare with the json banner_slide, and what doesn't have in json will be delete.
But what if i have 1000 record in the table? So these 2 way are not really good.