我需要删除所有CMS页面。
这是代码:
命名空间Soon \ Core \ Setup;
使用Magento \ Cms \ Api \ PageRepositoryInterface;
使用Magento \ Framework \ Api \ SearchCriteriaInterface;
Cms类
{
/ **
* @var SearchCriteriaInterface
* /
私人$ searchCriteria;
/ **
* @var PageRepositoryInterface
* /
私人$ cmsPageRepository;
/ **
* Cms构造函数。
* @param SearchCriteriaInterface $ searchCriteria
* @param PageRepositoryInterface $ cmsPageRepository
* /
公共功能__construct(
SearchCriteriaInterface $ searchCriteria,
PageRepositoryInterface $ cmsPageRepository
)
{
$ this-> searchCriteria = $ searchCriteria;
$ this-> cmsPageRepository = $ cmsPageRepository;
}
/ **
*删除所有现有的CMS页面
* /
公共函数cleanCmsPages()
{
$ cmsPageCollection = $ this-> cmsPageRepository
-> getList($ this-> searchCriteria)
-> getItems();
foreach($ cmsPageCollection as $ cmsPage){
$ this-> cmsPageRepository-> delete($ cmsPage);
}
}
}
因此,调用\Soon\Core\Setup\Cms::cleanCmsPages应删除所有CMS页面。
但是这样做的时候,我得到这个错误:
Argument 1 passed to Magento\Cms\Model\PageRepository::delete() must implement interface Magento\Cms\Api\Data\PageInterface, array given
所以我把$cmsPage用过的东西丢在了我的foreach ($cmsPageCollection as $cmsPage)里面,看来确实$cmsPage是一个数组。
我深入研究了代码:
\Magento\Cms\Api\PageRepositoryInterface::getList由实现\Magento\Cms\Model\PageRepository::getList。
然后在中\Magento\Cms\Model\PageRepository::getList,我们可以看到以下代码:
$ pages [] = $ this-> dataObjectProcessor-> buildOutputDataArray(
$ pageData,
'Magento \ Cms \ Api \ Data \ PageInterface'
);
}
$ searchResults-> setItems($ pages);
如果我是对的,那么这段代码将创建一个填充该$pages数组的数组。所以这段代码可以解释为什么$cmsPage是数组!
但...
通过阅读的@return陈述\Magento\Cms\Api\PageRepositoryInterface::getList,我们可以看到@return \Magento\Cms\Api\Data\PageSearchResultsInterface。
然后,通过阅读的@return声明\Magento\Cms\Api\Data\PageSearchResultsInterface::getItems,我们可以看到\Magento\Cms\Api\Data\PageInterface[]!
因此$cmsPage,我的foreach循环中的in 应该是\Magento\Cms\Api\Data\PageInterface可以正确传递给的实现\Magento\Cms\Api\PageRepositoryInterface::delete。
谁错了
- 我无法正确阅读/理解@api注释和代码的人
- Magento谁没有在其@api类中给出正确的注释...还是没有实现应有的接口。
此分析适用于CMS Page API,但也适用于CMS Block API。