Search code examples
dependency-injectiontypo3typo3-11.x

TYPO3 v 11 : dependency injection generates Object of class Aip\Bit3\Domain\Repository\AbstractRepository could not be converted to string


I'm trying to port a backend module from TYPO3 10 to TYPO3 11. I have already made dependency injection working moving the repository variable from protected to public, but this way of working is already deprecated and will be removed in TYPO3 12

I used the guide here to define a repository variable in my controller and instantiate it:

class ModuleConfController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
/**
 * @var \Aip\Bit3\Domain\Repository\AbstractRepository
 *
 */
private ?AbstractRepository $abstractRepository = null;

public function injectAbstractRepository(AbstractRepository $abstractRepository)
{
    $this->$abstractRepository = $abstractRepository;
}

I have also defined a Services.yaml file with this content:

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Aip\Bit3\:
    resource: '../Classes/*'
    exclude: '../Classes/Domain/Model/*'

The code seems to work since the inject injectAbstractRepository is called and the parameter is populated with an instance of the class AbstractRepository.

But instead of assinging the value to the class field it throws an exception

Object of class Aip\Bit3\Domain\Repository\AbstractRepository could not be converted to string

Can someone shed some light?


Solution

  • The comment made by Thomas Löffler solved the problem. I had improperly used cut and paste to write the wrong line and was mislead by the error message.

    You need to remove the $ and change the line to $this->abstractRepository – Thomas Löffler