【问题标题】:Create and save a value object in CommandController在 CommandController 中创建并保存一个值对象
【发布时间】:2014-05-19 09:45:34
【问题描述】:

我试图在我的 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;
}

}

更新 2:

试过了:

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

和:

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

没有效果:(

【问题讨论】:

  • 你应该使用对象管理器来获取一个新的对象实例。 $this->objectManager->get($objectName) 请发布您的 Period 课程
  • 没有人得到答案? :(
  • 您是否已经尝试通过 objectManager 创建/获取对象?并尝试从 AbstractEntity 而不是 AbstractValueObject 扩展 Period 类。我不确定 ValueObject 是否已完全实现。
  • 好的...我创建了一个 PeriodRepository 并将类更改为“AbstractEntity”,现在一切正常。但恕我直言,这是一个奇怪的解决方案。

标签: typo3 extbase typo3-6.1.x


【解决方案1】:

FeWo 中的 Period 类型是否正确? 它必须是这样的:

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

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    相关资源
    最近更新 更多