在v7中TYPO3 Extbase依赖注入错误/错误?

时间:2017-10-11 06:28:26

标签: typo3 extbase typo3-7.6.x

我目前正在升级extbase-extension以兼容TYPO3 v7, 并且有一个非常奇怪的extbase行为,我根本就没有任何线索。

在BackendController中,必须更新派生模型, 看起来像这样:

/**
 * action update
 *
 * @param \Vendor\MyExt\Domain\Model\Thing $thing
 * @return void
 */
public function updateAction(\Vendor\MyExt\Domain\Model\Thing $thing) {
    if ($this->request->hasArgument('exit')) {
        $this->redirect('list');
        exit;
    }

    $this->setFalItems($thing);
    $this->updateStuff($thing);
    $this->updateTypeModel($thing);
    //...
}

protected function updateTypeModel( \Vendor\MyExt\Domain\Model\Thing $thing ) {
    //...
    $objectManager    = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
    $mytypeRepository = $this->objectManager->get('Vendor\MyExt\Domain\Repository\TypeWhateverRepository');
    $typeModel        = $mytypeRepository->findByUid( $TypeId );
    //...

在第6版中,vardump( $typemodel )显示了相应的对象,Vendor\MyExt\Domain\Model\TypeWhatever

在第7版中,vardump( $typemodel )显示父对象,Vendor\MyExt\Domain\Model\Thing

为什么它在v6中运行? 为什么完全相同的代码不再适用于v7?

[梦见夜晚可怕的虫子]

我挖得更深一点,这个问题与依赖注入有某种关系。:

/**
 * typeWhateverRepository
 *
 * @var \Vendor\MyExt\Domain\Repository\TypeWhateverRepository
 * @inject
 */
protected $typeWhateverRepository;

protected function updateTypeModel(\Vendor\MyExt\Domain\Model\Thing $thing) {
//  $typeWhateverRepository = $this->objectManager->get('Vendor\\MyExt\\Domain\\Repository\\TypeWhateverRepository');
    $typeModel = $this->typeWhateverRepository->findByUid($thing->getTypeId());

    ->  still the same problem, 
    ->   Call to undefined method Vendor\MyExt\Domain\Model\Thing::setWhatever() 

所以,DI根本没有工作,Grmpf。 让DI正确的其他先决条件是什么?

(顺便说一句,在测试之间,我取消并重新安装ext,通过installtool清除所有缓存。)

提前谢谢。

2 个答案:

答案 0 :(得分:0)

首先......让我们做一些清理......

我建议使用您的存储库注入:

/**
 * seminarRepository
 *
 * @var \Vendor\MyExt\Domain\Repository\TypeWhateverRepository
 */
protected $typeWhateverRepository;

/**
 * @param \Vendor\MyExt\Domain\Repository\TypeWhateverRepository $typeWhateverRepository
 */
public function injectTypeWhateverRepository(TypeWhateverRepository $typeWhateverRepository)
{
  $this->typeWhateverRepository= $typeWhateverRepository;
}

然后我会使用从ThingType的关系,这样您就不必从存储库中获取这些内容:

/**
 * @lazy
 * @var \Vendor\MyExt\Domain\Model\TypeWhatever
 */
protected $typeWhatever = null;

/**
 * @return \Vendor\MyExt\Domain\Model\TypeWhatever $typeWhatever
 */
public function getTypeWhatever()
{
  return $this->typeWhatever;
}

/**
 * @param \Vendor\MyExt\Domain\Model\TypeWhatever $typeWhatever
 *
 * @return void
 */
public function setTypeWhatever(TypeWhatever $typeWhatever)
{
  $this->typeWhatever = $typeWhatever;
}

在你的事情TCA中比:

'type_whatever' => [
  'exclude' => 0,
  'label'   => 'LLL:EXT:my_ext/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_thing.type_whatever',
  'config'  => [
    'type'          => 'select',
    'foreign_table' => 'tx_myext_domain_model_typewhatever',
    'items'         => [

 ['LLL:EXT:my_ext/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_thing.choose', 0],
    ],
    'minitems'      => 1,
    'maxitems'      => 1,
  ],
],

答案 1 :(得分:0)

The Solution to this is trivial, but was hard to find, since I was doing an Extension Update.

the extbase-typoscript setup was missing the subclasses definition m)

extbase setup is usually found in the file
typo3conf/ext/my_ext/Configuration/TypoScript/setup.txt:

config.tx_extbase.persistence.classes {
    Vendor\MyExt\Domain\Model\Thing {
         subclasses {
                 0 = Vendor\MyExt\Domain\Model\TypeWhatever
         }
    }
}

Also note that it is necessary for the class to have a proper 'extends' definition in the Model file.

I still wonder why it worked in v6 at all - but well, nevermind.