Ở các bài trước chúng ta đã tạo được một form để nhập dữ liệu trong admin. Trong bài viết này tôi sẽ hướng dẫn các bạn thêm, sửa, xóa dữ liệu trong Magento 2.
Save, Edit dữ liệu
Ở bài trước chúng ta đã tạo được một form như bên dưới:

- Tạo Controller dùng để save và edit
Lưu ý ở bài tạo form chúng ta thêm đường dẫn. Khi submit form dữ liệu sẽ được gửi đến controller helloworld/post/save. Các bạn có thể xem lại bài viết tại đây.
ViMagento/HelloWorld/Controller/Adminhtml/Post/Save.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<?php
namespace ViMagentoHelloWorldControllerAdminhtmlPost;
use ViMagentoHelloWorldModelPostFactory;
use MagentoBackendAppAction;
/**
* Class Save
* @package ViMagentoHelloWorldControllerAdminhtmlPost
*/
class Save extends Action
{
/**
* @var PostFactory
*/
private $postFactory;
/**
* Save constructor.
* @param ActionContext $context
* @param PostFactory $postFactory
*/
public function __construct(
ActionContext $context,
PostFactory $postFactory
) {
parent::__construct($context);
$this->postFactory = $postFactory;
}
public function execute()
{
$data = $this->getRequest()->getPostValue();
$id = !empty($data[‘post_id’]) ? $data[‘post_id’] : null;
$newData = [
‘name’ => $data[‘name’],
‘status’ => $data[‘status’],
‘content’ => $data[‘content’],
];
$post = $this->postFactory->create();
if ($id) {
$post->load($id);
}
try {
$post->addData($newData);
$post->save();
$this->messageManager->addSuccessMessage(__(‘You saved the post.’));
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__($e->getMessage()));
}
return $this->resultRedirectFactory->create()->setPath(‘helloworld/post/index’);
}
}
|


Ở trên tôi dùng $this->getRequest()->getPostValue() để lấy được dữ liệu khi submit gửi lên. Sau đó tôi kiểm tra xem trong dữ liệu gửi lên có id hay không. Nếu có tức là đang edit, nếu không tức là thêm mới. Sau đó dùng model để lưu dữ liệu vào database, thêm câu thông báo sau đó chuyển hướng trang về grid.
Delete dữ liệu
Khi chúng ta tạo grid listing ở bài trước chúng ta đã thêm selectionsColumn và delete action dẫn đến controller helloworld/post/index như hình bên dưới:

ViMagento/HelloWorld/Controller/Adminhtml/Post/Delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<?php
namespace ViMagentoHelloWorldControllerAdminhtmlPost;
use MagentoBackendAppAction;
use ViMagentoHelloWorldModelResourceModelPostCollectionFactory;
use ViMagentoHelloWorldModelPostFactory;
use MagentoUiComponentMassActionFilter;
use MagentoBackendModelViewResultRedirectFactory;
class Delete extends Action
{
private $postFactory;
private $filter;
private $collectionFactory;
private $resultRedirect;
public function __construct(
ActionContext $context,
PostFactory $postFactory,
Filter $filter,
CollectionFactory $collectionFactory,
RedirectFactory $redirectFactory
)
{
parent::__construct($context);
$this->postFactory = $postFactory;
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->resultRedirect = $redirectFactory;
}
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$total = 0;
$err = 0;
foreach ($collection->getItems() as $item) {
$deletePost = $this->postFactory->create()->load($item->getData(‘post_id’));
try {
$deletePost->delete();
$total++;
} catch (LocalizedException $exception) {
$err++;
}
}
if ($total) {
$this->messageManager->addSuccessMessage(
__(‘A total of %1 record(s) have been deleted.’, $total)
);
}
if ($err) {
$this->messageManager->addErrorMessage(
__(
‘A total of %1 record(s) haven’t been deleted. Please see server logs for more details.’,
$err
)
);
}
return $this->resultRedirect->create()->setPath(‘helloworld/post/index’);
}
}
|


Sử dụng filter class để lấy được các record mà chúng ta tick chọn để xóa và dùng model để load id lấy được và dùng ->delete() để xóa. Sau đó thì chuyển hướng về trang grid của chúng ta. Ở bài sau tôi sẽ hướng dẫn các bạn tạo column edit trong grid dùng để chuyển đến trang edit.