Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb not hydrating references #385

Open
marcusorjames opened this issue Jan 30, 2018 · 2 comments
Open

mongodb not hydrating references #385

marcusorjames opened this issue Jan 30, 2018 · 2 comments

Comments

@marcusorjames
Copy link

I have a mongodb document with a referenced document:

/**
 * Class User
 *
 * @MongoDB\Document
 */
class User
{

    /**
     * @MongoDB\Field(type="string")
     */
    protected $email;

    /**
     * @var Contact
     * @MongoDB\ReferenceOne(targetDocument="Document\Contact")
     */
    private $contact;

    /**
     * {@inheritdoc}
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @return Contact
     */
    public function getContact(): Contact
    {
        return $this->contact;
    }

    /**
     * @param Contact $contact
     * @return User
     */
    public function setContact(Contact $contact): User
    {
        $this->contact = $contact;

        return $this;
    }
}

When running a test:

public function testSample()
{
        $fixtures = $this->loadFixtures([
            'DataFixtures\UserFixtures',
        ], null, 'doctrine_mongodb');

        $repository = $fixtures->getReferenceRepository();
        /** @var User $user */
        $user  = $repository->getReference('guest-user');
        $user->getEmail(); // Have to do this to hydrate
        $user->getContact();
 }

I have to call getEmail(), if I don't then getContact() returns null. It doesn't seem to be hydrating contact until I call another getter. As soon as I put getEmail() in before getContact() it returns a contact perfectly. Any idea on this? Thank you

@alexislefebvre
Copy link
Collaborator

alexislefebvre commented Jan 30, 2018

Can you please show the content of DataFixtures\UserFixtures? Just to check that something like persist() is not missing.

Please note that in your code you call $user->getContact();, this line returns nothing, is an assertion missing?

@marcusorjames
Copy link
Author

As requested. Note I have simplified the namespaces for sensitivity reasons. Persist is not missing :)

The assertion is missing but is irrelevant. It would be something like:

$contact = $user->getContact();
$this->assertInstanceOf(Contact::class,  $contact);
namespace DataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Document\Contact;
use Document\User;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class UserFixtures extends AbstractFixture implements FixtureInterface
{

    /**
     * @var UserPasswordEncoderInterface
     */
    private $encoder;

    public function __construct(UserPasswordEncoderInterface $encoder)
    {
        $this->encoder = $encoder;
    }

    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param ObjectManager $manager
     * @throws \Doctrine\Common\DataFixtures\BadMethodCallException
     */
    public function load(ObjectManager $manager)
    {
        $this->loadContact($manager);
    }

    /**
     * @param ObjectManager $manager
     * @throws \Doctrine\Common\DataFixtures\BadMethodCallException
     */
    private function loadContact(ObjectManager $manager)
    {
        $contact = new Contact();
        $contact
            ->setGivenName('Ed')
            ->setFamilyName('Winchester');

        $manager->persist($contact);

        $user = new User();
        $user
            ->setEmail('[email protected]')
            ->setPassword($this->encoder->encodePassword($user, 'hiemed'))
            ->setContact($contact)
            ->addRole('ROLE_CONTACT');

        $manager->persist($user);
        $manager->flush();

        $this->addReference('guest-user', $user);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants