4

I have added a Repository interface in my Api directory and it's implementation in located under Models directory.

I have setup the preference for the interface in etc/di.xml.

When I run bin/magento setup:di:compile the following error is thrown in the console.

Source class "\Namespace\Module\Model\PostInterface" for "Namespace\Module\Model\PostRepository" generation does not exist.

I have no idea why the system is searching for "\Namespace\Module\Model\PostInterface" while I have mentioned clearly the location of the implementation class in di.xml.

Any suggestions are welcome with thanks !!

More Info:

When I remove the Repository implementation file i.e; app/code/Namespace/Module/Model/PostRepository.php (imlpements app/code/Namespace/Module/Api/PostRepositoryInterface.php), the error disappears.

Also, when I rename the file from app/code/Namespace/Module/Model/PostRepository.php to app/code/Namespace/Module/Model/PostRepositoryy.php, the error disappears.

Is there any conventions that needs to be followed while using Repositories ?

2
  • I get the same error and removing or renaming the file does not help. It's crazy. Sep 18, 2017 at 15:25
  • Did you add <preference for="Namespace\Module\Api\Data\PostInterface" type="Namespace\Module\Model\PostRepository" /> in your di.xml? Jul 7, 2022 at 7:01

5 Answers 5

3

After a lot of Code analysis and Google searches, I found the fix for this problem. Adding the following to the di.xml fixed this issue.

<type name="Magento\Framework\Model\Entity\RepositoryFactory">
        <arguments>
            <argument name="entities" xsi:type="array">
                <item name="Namespace\Module\Api\Data\PostInterface" xsi:type="string">Namespace\Module\Api\Data\PostRepositoryInterface
                </item>
            </argument>
        </arguments>
    </type>
1
  • Did you add <preference for="Namespace\Module\Api\Data\PostInterface" type="Namespace\Module\Model\PostRepository" /> in your di.xml? Jul 7, 2022 at 7:01
1

Remove var/generation and var/di folders Then try again with command

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy
1
  • I have followed these steps and the error still appears. Mar 23, 2017 at 6:46
1

I had this issue with a custom module, it was a typo in the composer.json file:

"autoload": {
    "files": [
        "registration.php"
    ],
    "psr-4": {
        "Vendor\\Namespace\\": ""
    }
}

in the Vendor\\Namespace. It might be useful for others.

0

This is an old post now, but for anyone else that's still experiencing this, a fix that seems to work is rather than defining the repository interface as a use statement, add its fully qualified name, for example:

use Vendor\Module\Interface;

class MyClass implements Interface

Becomes:

class MyClass implements \Vendor\Module\Interface

This fixed this for us when compiling repositories on M2 2.2.3

0

I've had this error after trying to generate di and it even persisted on any cli command for some reason.

Try to delete generated/*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.