【发布时间】:2015-10-12 13:27:57
【问题描述】:
我有一个事件创建扩展,其中包含为用户创建事件的选项。这是使用 extbase 存储库方法实现的。在活动创建表单中,我有一个开始日期、结束日期和应该重复活动的工作日。
例如,如果我将开始日期设置为 13-10-2015 并将结束日期设置为 30-10-2015。并将工作日选择为“星期三”。因此该事件将在 13 到 30 之间的所有“星期三”重复。
当我创建一个非重复事件时,它会正常工作。但是对于重复发生的事件,我已经实现了extbase存储库的克隆方法。
if (!empty($endDateRecurring) && !empty($recurringWeekDays)) {
$endDate = new \DateTime($endDateRecurring);
$startDate = $newEvent->getDate();
$startDate->setTime(0, 0);
$datetimeDiff = $startDate->diff($endDate);
for ( $event=1; $event <= $datetimeDiff->days; $event++ ) {
$checkDate = $newEvent->getDate()->add(new \DateInterval('P'.$event.'D'));
$dayOfWeekNo = date('w',$checkDate->getTimestamp());
if ( in_array( $dayOfWeekNo, $recurringWeekDays ) ) {
$eventProperties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties( $newEvent );
\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty( $eventProperties, 'date', $eventProperties['date']->add(new \DateInterval('P1D') ) );
$eventCopy = $this->objectManager->create('\TYPO3\EventCreate\Domain\Model\Event');
foreach ( $eventProperties as $propertyName => $propertyValue ) {
if (\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertySettable($eventCopy,
$propertyName) && !in_array($propertyName, array('uid','pid'))) {
$propertyValue = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty( $newEvent, $propertyName );
\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty( $eventCopy, $propertyName, $propertyValue );
}
}
//TODO: cloning doesn't work because the id is already set and it thus doesn't add a new object to the repository.
//either create a new object and copy all content properties, or find another way to add more events to the db for recurring days.
$this->eventRepository->add($eventCopy);
$this->objectManger->get('Tx_Extbase_Persistence_Manager')->persistAll();
}
}
}
这里的 $recurringWeekDays 是一个数组,其中包含选定的重复工作日 id(对于 Monday-1, Tuesday-2 像这样)。
问题是当有任意天数要插入时,只有一条记录插入到数据库中。在上述示例中,10 月 13 日至 30 日之间的 3 个星期三。但只插入了一条记录。
如果有人知道解决方案,请帮忙。
【问题讨论】:
标签: php repository typo3 extbase typo3-6.2.x