Magento 2.1 – Source Class Generation Does Not Exist Error in setup:di:compile

magento-2.1

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 ?

Best Answer

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>
Related Topic