我创建了自己的CRUD模块,其中包含类似于CMS页面的内联编辑操作,
一切正常,但是以EcgM2标准运行phpsniffer时,出现以下警告:
在循环中检测到模型LSD方法save()
如何避免这种情况?
注意:如果我“嗅探”上面链接的核心文件,则会出现相同的警告。如果有人需要,
这是我的execute
方法。但这与CMS页面控制器中的非常相似
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$postItems = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
return $resultJson->setData([
'messages' => [__('Please correct the data sent.')],
'error' => true,
]);
}
foreach (array_keys($postItems) as $authorId) {
/** @var \Sample\News\Model\Author $author */
$author = $this->authorRepository->getById((int)$authorId);
try {
$authorData = $this->filterData($postItems[$authorId]);
$this->dataObjectHelper->populateWithArray($author, $authorData , AuthorInterface::class);
$this->authorRepository->save($author);
} catch (LocalizedException $e) {
$messages[] = $this->getErrorWithAuthorId($author, $e->getMessage());
$error = true;
} catch (\RuntimeException $e) {
$messages[] = $this->getErrorWithAuthorId($author, $e->getMessage());
$error = true;
} catch (\Exception $e) {
$messages[] = $this->getErrorWithAuthorId(
$author,
__('Something went wrong while saving the author.')
);
$error = true;
}
}
return $resultJson->setData([
'messages' => $messages,
'error' => $error
]);
}