I am using Symfony and in one of my entity classes, I faced a problem with missing the repository class. I have a Booking class with a repository class named BookingRepository.
class BookingRepository extends EntityRepository
Also, I have developed many functions in this Repo class and was working about two days ago.
Now, I don't know why the Booking class does not know the BookingRepository class as a repository.
What happened for my code?
Related
My problem is the $this->getUser(); method (called from Controller) from Symfony Security can't get my User Repository.
The "App\Model\ODM\Repository\UserRepository" document repository implements "Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine_mongodb.odm.repository_service".
I have a CompilerPass that get my Repositories and add them to Container Definitions depending on some env value.
I add the Repo with "generic" Interfaces (valid for both ODM and ORM) as keys AND with the Repo FQCN as keys, so they should be accessible via both the generic Interface and the true Repo name, but I'm working only with ODM for now.
Also, I "tag" my repo with doctrine_mongodb argument, to extends the ServiceDocumentRepository baseClass.
I don't include the compilerPass because the class is quite "heavy", but if needed I'll include it.
Here is my UserRepo
class UserRepository extends BaseRepository implements RepositoryInterface, UserRepositoryInterface
{
/**
* UserRepository constructor.
*
* #param ManagerRegistry $managerRegistry
*/
public function __construct(ManagerRegistry $managerRegistry)
{
parent::__construct($managerRegistry, User::class);
}
...
My BaseRepo (just a few methods in here, just the extends is interesting)
abstract class BaseRepository extends ServiceDocumentRepository implements RepositoryInterface
{
...
It's working great "in-app", I could create a User on a registration page, but using $guardHandler->authenticateUserAndHandleSuccess() method after register, i'm redirected to my homepage where the $this->getUser(); is, and then I have the error message posted above.
After some research, it seems the problem is in Doctrine\Bundle\MongoDBBundle\Repository\ContainerRepositoryFactory
The container of this class doesn't seem to have any of my Repo definition.
I surely missed something but I don't know where.
I remember making something similar few years ago but extending DocumentRepository and not ServiceDocumentRepository.
If you need any other information, let me know.
I feel really dumb about this question now that I found the solution ...
It was LITTERALY in the error message, I just had to add doctrine_mongodb.odm.repository_service to my Repo's Definition, in my Compiler Pass
...
$definition->addArgument(new Reference('doctrine_mongodb'));
$definition->addTag('doctrine_mongodb.odm.repository_service');
...
Thanks to the people who looked at my problem and sorry for wasting your time.
I Hope this answer could help others peoples that register Repo in a CompilerPass
(I will set this answer as the solution asap, in 2 days)
Did you enable autowire in your services.yml?
services:
_defaults:
autowire: true
autoconfigure: true
App\Repository\:
resource: '../src/Repository/*'
Ill put my solution in case someone else has this problem and the one above doesn't work. For me what worked was to put the whole path to the repository when it was declared in the Document class. Like so:
// /src/Document/User
/**
* #MongoDB\Document(collection="users", db="database", repositoryClass=\App\Repository\UserRepository::class)
* #MongoDBUnique(fields="email")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
I didn'nt have to add anything in my Compiler Pass. I added the full path to the repository class in its declaration and it instantly worked. Hope that it helps.
I am working on a SilverStripe project. Project SilverStripe version is 4.4.4. In my project, I am trying to replace a class that injected for an interface. Both the interface and the class are part of the framework.
In the framework code, it has the following class
class NaturalFileIDHelper implements FileIDHelper
As you can see, the NaturalFileIDHelper is implementing the FileIDHelper interface. What I want to do is that I want to replace all the NaturalFileIDHelper instances with my customer class called CustomFileIDHelper class that is also implementing the FileIDHelper interface.
This is what I did.
I created a custom class called, CustomFileIDHelper that is implementing the FileIDHelper interface.
Then I added the following code snippet within the mysite.yml.
SilverStripe\Assets\FilenameParsing\FileIDHelper:
class: CustomFileIDHelper
Then I rebuild the project. But my project is still using the NaturalFileIDHelper class that comes with the framework. It seems like the CustomFileIDHelper class is not used.
How can I get it working? Is it possible to do that?
I want to replace all the NaturalFileIDHelper instances with my customer class called CustomFileIDHelper class
If you want to replace all NaturalFileIDHelper instances, then that's the class you should override (not the FileIDHelper). Also the .yml config you have needs to be passed to the Injector. (https://docs.silverstripe.org/en/4/developer_guides/extending/injector/)
SilverStripe\Core\Injector\Injector:
SilverStripe\Assets\FilenameParsing\NaturalFileIDHelper:
class: CustomFileIDHelper
But this will only work if the object is instantiated through the ClassName::create() function, and I found this piece of code in the framework:
(LegacyThumbnailMigrationHelper.php)
$defaultFileIDHelper = new NaturalFileIDHelper(), // Injector is not called here.
You will need to override the classes which call new NaturalFileIDHelper() and modify it to NaturalFileIDHelper::create(), you're able to call ::create() because the NaturalFileIDHelper class have the use Injectable; trait.
Also instead of going through the troubles of overriding classes which call new NaturalFileIDHelper(), you can create a pull request to the framework repository with your NaturalFileIDHelper::create() changes instead, as it would be an improvement to the current framework code (it enables the use of dependency injection).
I have added a Nugget package Identity.frameworkcore to my project and I have used the IdentityUser class, but it seems that it is empty.
I am also looking at the tutorial and in there it seems that the class is quite full
Any tips?
The situations' setup is like this:
1) I have two bundles in which I would like to use the same entity. So as suggested in this question and then in this article, I created the CommonEntitiesBundle where the desired Entity lives.
2) Then I described the model interface in both bundles I want to use the Entity in. So far so good.
Now I would like to use the interfaced Entity, just like I would use a 'bundle-native' one like this:
$clientData = new Client(); // the Entity class
$client->setName('Greg');
How should I approach it when there is only the interface available?
I used the interface in this controller.
In the Symfony's example there is only a use of the class via the ORM annotations.
An interface is not a class. it is instead a list of minimal necessary methods a class with 'implements [interfaceName]' must have.
Instead you could provide a example class with your bundle and use that one by default. Add configuration in app/config.yml where you can change the classname. each derived class must implement the interface for consistence.
I'm building an application in Symfony 2.2.
I use a custom repository class for one of my entities. I include it like this:
#ORM\Entity(repositoryClass="MyApp\MainBundle\Entity\CategoryRepository")
I want to use methods on my category entites from two other repository classes.
#ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
#ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
The entity, however, only expects one repositoryClass. How can I use all three of these repositories for the same entity?
I believe, you can only have one repository per Entity.
So you have referenced
ORM\Entity(repositoryClass="MyApp\MainBundle\Entity\CategoryRepository")
If you must use another repositories within the repository,
You can get the EntityManager for those Repositories (if not same) eg: $em2 = whatEverIsTheEntity()
Now you can have proxy methods in CategoryRepository to the methods in the repository you want to use. eg: $em2->getMethodFromEm2().
Note: But I wouldn't prefer above solution unless there is really a need. I would rather create a service layer which has Doctrine Entity Manager as Dependency Injected. This service layer will connect to all the repositories and collect final result whatever you want to achieve (to be used in the controller). This way, the code will be much cleaner and testable as well.
Hope this helps.
Repositories are class (so entity) reserved. If you inherit from super classes (even if those classes doesn't inherit directly, but repos can), than you can make your repository (CategoryRepository) extended by other two (one extend the other, since PHP doesn't allow more than one class extension).
Pretty much something like that
class NestedTreeRepository extends SortableRepository
{
}
class CategoryRepository extends NestedTreeRepository
{
}
don't know if this fits your needs, but this a way (and I'll not surprised if is the only one) for realize what you're trying to do here