在CommandController中创建并保存值对象

时间:2014-04-07 08:42:48

标签: typo3 extbase typo3-6.1.x

我试图在 ImportCommandController.php 中创建并保存一个值对象,但只保存该实体。

让我展示一些代码:

// Create Entity "Fewo"
$fewo = new Fewo();
$fewo->setTitle('MyFewo');
...

// Create Value Object "Period"
$period = new Period();
$period->setTitle('MyTestTitle');
...

$fewo->addPeriod($period);

$this->fewoRepository->add($fewo);
$this->persistenceManager->persistAll();

现在Fewo在数据库中,但是期间表仍然是空的。我无法找到我的错误......

更新

这是期间模型:

<?php

namespace TYPO3\Fewo\Domain\Model;

class Period extends \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject {

/**
 * Name der Saison
 *
 * @var \string
 */
protected $name;

/**
 * Von
 *
 * @var \DateTime
 */
protected $begin;

/**
 * Bis
 *
 * @var \DateTime
 */
protected $end;

/**
 * rentalcharges
 *
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge>
 */
protected $rentalcharges;

/**
 * __construct
 *
 * @return Period
 */
public function __construct() {
    //Do not remove the next line: It would break the functionality
    $this->initStorageObjects();
}

/**
 * Initializes all ObjectStorage properties.
 *
 * @return void
 */
protected function initStorageObjects() {
    /**
     * Do not modify this method!
     * It will be rewritten on each save in the extension builder
     * You may modify the constructor of this class instead
     */
    $this->rentalcharges = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}

/**
 * Returns the name
 *
 * @return \string $name
 */
public function getName() {
    return $this->name;
}

/**
 * Sets the name
 *
 * @param \string $name
 * @return void
 */
public function setName($name) {
    $this->name = $name;
}

/**
 * Returns the begin
 *
 * @return \DateTime $begin
 */
public function getBegin() {
    return $this->begin;
}

/**
 * Sets the begin
 *
 * @param \DateTime $begin
 * @return void
 */
public function setBegin($begin) {
    $this->begin = $begin;
}

/**
 * Returns the end
 *
 * @return \DateTime $end
 */
public function getEnd() {
    return $this->end;
}

/**
 * Sets the end
 *
 * @param \DateTime $end
 * @return void
 */
public function setEnd($end) {
    $this->end = $end;
}

/**
 * Adds a Rentalcharge
 *
 * @param \TYPO3\Fewo\Domain\Model\Rentalcharge $rentalcharge
 * @return void
 */
public function addRentalcharge(\TYPO3\Fewo\Domain\Model\Rentalcharge $rentalcharge) {
    $this->rentalcharges->attach($rentalcharge);
}

/**
 * Removes a Rentalcharge
 *
 * @param \TYPO3\Fewo\Domain\Model\Rentalcharge $rentalchargeToRemove The Rentalcharge to be removed
 * @return void
 */
public function removeRentalcharge(\TYPO3\Fewo\Domain\Model\Rentalcharge $rentalchargeToRemove) {
    $this->rentalcharges->detach($rentalchargeToRemove);
}

/**
 * Returns the rentalcharges
 *
 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge> $rentalcharges
 */
public function getRentalcharges() {
    return $this->rentalcharges;
}

/**
 * Sets the rentalcharges
 *
 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge> $rentalcharges
 * @return void
 */
public function setRentalcharges(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $rentalcharges) {
    $this->rentalcharges = $rentalcharges;
}

}

UPDATE2:

尝试:

class Period extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {...}

$period = $this->objectManager->get('TYPO3\Fewo\Domain\Model\Period');

没有效果:(

1 个答案:

答案 0 :(得分:0)

是否为正确类型的FeWo期间? 它必须是这样的:

* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Fewo>