我写了一个命令来转换一些数据。为了简单起见,我使用了此类记录的存储库。例如,特定类别 (tx_myext_domain_model_category) 应转移到 sys_category。
当我得到一份已处理记录的列表时,阅读就起作用了。
然后我将数据从
$myext_category
传输到 $sys_category
并将其添加到存储库中:
$this->sysCategoryRepository->add($sysCategory);
$this->persistenceManager->persistAll();
但是数据库中没有生成任何记录。
我的类别记录代码:
<?php
namespace MyVendor\MyExt\Command;
use MyVendor\MyExt\Domain\Repository\CategoryRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\Category as SysCategory;
use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository as SysCategoryRepository;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use MyVendor\MyExt\Domain\Repository\CategoryRepository as MyExtCategoryRepository;
class ConvertEventsCommand extends Command {
/**
* @var MyVendor\MyExt\Domain\Repository\CategoryRepository
* @Inject
*/
protected $myExtCategoryRepository;
/**
* @var \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository
* @Inject
*/
protected $sysCategoryRepository;
/**
* @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
* @Inject
*/
protected $persistenceManager;
/**
* @const int
*/
protected $offsetCategories = 1000;
protected $io;
public function __construct(
MyExtCategoryRepository $myExtCategorRepository,
SysCategoryRepository $sysCategoryRepository,
PersistenceManager $persistenceManager
) {
parent::__construct();
$this->MyExtCategoryRepository = $myExtCategorRepository;
$this->sysCategoryRepository = $sysCategoryRepository;
$this->persistenceManager = $persistenceManager;
// reading repositories:
$querySettings = $this->myExtCategoryRepository->createQuery()->getQuerySettings();
$querySettings->setRespectStoragePage(false);
$querySettings->setIgnoreEnableFields([]);
$querySettings->setIncludeDeleted(false);
$this->myExtCategoryRepository->setDefaultQuerySettings($querySettings);
// writing repositories:
$querySettings2 = $this->sysCategoryRepository->createQuery()->getQuerySettings();
$querySettings2->setStoragePageIds([123]);
$this->sysCategoryRepository->setDefaultQuerySettings($querySettings2);
}
/**
* Configure the command by defining the name, options and arguments
*/
protected function configure()
{
$this->setDescription('converts data from ext:my_ext to sys_category')
->setHelp('gets all record from extension my_ext and convert it to records in sys_category ');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Bootstrap::initializeBackendAuthentication();
$this->io = new SymfonyStyle($input, $output);
$this->io->title($this->getDescription());
$this->io->writeln('### copy categories ###');
$this->copyCategories();
$this->io->writeln('### done ###');
return Command::SUCCESS;
}
/**
* copy categories to sys_categories
*/
public function copyCategories() {
$myExtCategories = $this->myExtCategoryRepository->findAll();
// create a new parent category
$sysParentCategory = new \TYPO3\CMS\Extbase\Domain\Model\Category();
$sysParentCategory->_setProperty('uid',$this->offsetCategories);
$sysParentCategory->setPid(123);
$sysParentCategory->setTitle('Event-Categories');
$this->sysCategoryRepository->add($sysParentCategory);
$this->persistenceManager->persistAll();
foreach ($myExtCategories as $myExtCategory) {
$this->io->writeln('category '.$myExtCategory->getUid().': '.$myExtCategory->getName());
$sysCategory = new \TYPO3\CMS\Extbase\Domain\Model\Category();
$sysCategory->_setproperty('uid',$myExtCategory->getUid()+$this->offsetCategories);
$sysCategory->setPid($myExtCategory->getPid());
$sysCategory->_setProperty('hidden',$myExtCategory->_getProperty('hidden'));
$sysCategory->_setProperty('starttime',$myExtCategory->_getProperty('starttime'));
$sysCategory->_setProperty('endtime',$myExtCategory->_getProperty('endtime'));
$sysCategory->setTitle($myExtCategory->getName());
$sysCategory->setParent($sysParentCategory);
$this->sysCategoryRepository->add($sysCategory);
$this->persistenceManager->persistAll();
}
}
}
缺少什么? 怎么了?
在不设置
uid
的情况下,记录会被写入,但会增加新的 uid。因为我需要确切的 uid,而不是一些随机的 uid [1],因为它们应该在其他记录中引用,这将导致大量交换 uid。
我尝试在记录写入后立即更新
uid
:
if ($doInsert) {
$this->sysCategoryRepository->add($sysCategory);
$this->persistenceManager->persistAll();
// now $syscategory should have the new uid
$newUid = $sysCategory->getuid();
$oldUid = $myExtCategory->getuid();
if ($newUid != $oldUid) {
// change uid $newUid to $oldUid of myExtCategory.....
$updateSql = 'UPDATE sys_category SET uid = '.$oldUid.' WHERE uid = '. $newUid;
//$this->io->writeln($updateSql.';');
$q = $this->sysCategoryRepository->createQuery();
$q->statement($updateSql);
$queryResult = $q->execute();
$this->persistenceManager->clearState();
}
}
但数据库没有变化。
我也无法插入或更新有关我也想保留的enableFields(
hidden
,deleted
,starttime
,endtime
)的任何字段。
[1] 因为有已删除的记录,所以即使我使用
->setDefaultOrderings(['uid'=>QueryInterface::ORDER_ASCENDING]);
来获取 findAll()
的 uid 增加,也会有间隙
您是否尝试不使用 sysCategoryRepository 来更新
uid
?
比如这样?
// ... your code
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$q = $connectionPool->getQueryBuilderForTable('sys_category');
$connection = $connectionPool->getConnectionByName('Default');
$updateSql = 'UPDATE sys_category SET uid = '.$oldUid.' WHERE uid = '. $newUid;
$statement = $connection->executeQuery($updateSql);
// ... your code