V
V
Vitaliy2021-07-30 15:13:44
Doctrine ORM
Vitaliy, 2021-07-30 15:13:44

How to properly get data from the Repository class using Doctrine ORM without frameworks?

There is a class inherited from EntiryRepository.
The goal is to get the user's data using $email as the key and then call the getUserData() method by including the UserRepository in another class.

The problem is that when connecting the UserRepository , it also requires Required parameters '$em, $class' missing

class UserRepository extends EntityRepository
{
    public function getUserData(): array
    {
        $user = new User();
        return $this->_em->getRepository(User::class)->findOneBy($user->getEmail());
    }
}


The class itself in which the getUserData () method is called
public function getCurrentUserData(): array
    {
        $user = new User();
        $userRepository = new UserRepository();

        return [
            $user->getName(),
            $user->getLastName(),
            $user->getEmail(),
            $user->getMobileNumber(),
            $user->getDateTime(),
        ] ;
    }


1) How to correctly implement class dependencies in order to avoid errors during initialization?
2) Is there a real example, not using frameworks, the implementation of the Entity and Repository classes and their bundle?

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2021-07-30
@BoShurik

Repositories must be obtained from EntityManager, not created by ourselves.
To do this, you need to UserRepositoryregister the entity in the mapping
@ORM\Entity(repositoryClass="UserRepository")
More details in the documentation
https://www.doctrine-project.org/projects/doctrine...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question