typo3fff
typo3fff

Reputation: 21

Extbase: inject repository into domain model

I am currently working with Typo3 6.2.10 and Extbase.

I am trying to inject a repository into my domain model like this:

class MyModel extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
    /**
     * @inject
     * @var \Vendor\Package\Domain\Repository\SomeRepository
     */
    protected $someRepository;
}

However, $this->someRepository is always null. Injecting repositories into controllers always works though.

Thank you in advance!

Upvotes: 0

Views: 1928

Answers (2)

Dimitri Lavrenük
Dimitri Lavrenük

Reputation: 4889

Keep in mind, that you must omit the first \ in the class name when useing the $this->objectManager->get(xxx); or this line will throw an exception in typo3 7.x+.

$this->objectManager->get('Vendor\Package\Domain\Model\MyModel');

Also the Backslash is an escape character, so it is safer to escape the backslash or just use the static constant of the class

escaped:

$this->objectManager->get('Vendor\\Package\\Domain\\Model\\MyModel');

Using the static class name:

$this->objectManager->get(\Vendor\Package\Domain\Model\MyModel::class);

I prefer to use the last method, since you can see if you did a typo (depends on the IDE)

Upvotes: 1

typo3fff
typo3fff

Reputation: 21

Sorry, I found the solution myself.

I tried to instantiate the model using new keyword which (ofc) doesn't work.

I had to use $this->objectManager->get('\Vendor\Package\Domain\Model\MyModel'); instead.

Upvotes: 0

Related Questions