ZF2 + Doctrine2: Entity is saved but finding it returns no result #2519

Closed
dranzd opened this Issue Nov 5, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@dranzd
Contributor

dranzd commented Nov 5, 2015

ZF2 + Doctrine2: Entity is saved but finding it returns no result

I have a sample test below which fails when I try to retrieve the persisted entity
using the entity manager taken from ZF2's service manager. The entity is persited
and verified that the ID is generated along with the data included. But when finding
for that entity either by ID lookup or other field lookup, it fails to return the
persisted entity.

// configuration
class_name: FunctionalTester
modules:
    enabled:
        # add framework module here
        - \Helper\Functional
        - Asserts
        - ZF2
        - Doctrine2:
            depends: ZF2

// test
class UserCest
{
    ...

    public function testSaveAndRetrieveUser(FunctionalTester $I)
    {
        $app = $I->getModuleOf('ZF2')->application;

        // mapper that points to My\Entity\User, table is using InnoDB
        $user = new User();
        $I->persistEntity($user, [
            'name'       => 'User Name',
            'createdOn'  => new \Datetime(),
            'modifiedOn' => new \Datetime(),
        ]);

        $I->seeInRepository('My\Entity\User', [
            'name' => 'User Name',
        ]); // this passes with ID generated

        // $em here is a different instance from Doctrine2::$em
        $em = $app->getServiceManager()->get('Doctrine\ORM\EntityManager');
        $userRepository = $em->getRepository('My\Entity\User');

        $users = $userRepository->findBy(['name' => 'User Name']);

        $I->assertEquals(count($users), 1);  // this fails and returns 0
    }
}

I was trying to debug it and found out that the entity manager ZF2 holds is different
from the one Doctrine2 holds. I don't know if having 2 instances of entity manager
makes a difference when one is stored in the Doctrine 2 instance and looking up with
ZF2's instance.

I tried to update the code at ZF2 modules (ZF2::_getEntityManager) from:

public function _getEntityManager()
{
    return $this->grabServiceFromContainer('Doctrine\ORM\EntityManager');
}

to:

public function _getEntityManager()
{
    if ($this->application) {
        return $this->application->getServiceManager()->get('Doctrine\ORM\EntityManager');
    }
    return $this->grabServiceFromContainer('Doctrine\ORM\EntityManager');
}

and this makes the test pass.

I don't know if this is a bug or I just don't have my configurations entered correctly. If
anyone has any idea where I am off, any help is appreciated.

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Nov 5, 2015

Member

grabServiceFromContainer executes Application::init($this->applicationConfig)

_before method executes it first and assigns it to $this->application
$this->application = Application::init($this->applicationConfig);
wouldn't it be more correct to use $this->application in grabServiceFromContainer?

Member

Naktibalda commented Nov 5, 2015

grabServiceFromContainer executes Application::init($this->applicationConfig)

_before method executes it first and assigns it to $this->application
$this->application = Application::init($this->applicationConfig);
wouldn't it be more correct to use $this->application in grabServiceFromContainer?

@dranzd

This comment has been minimized.

Show comment
Hide comment
@dranzd

dranzd Nov 6, 2015

Contributor

If the only purpose of the grabServiceFromContainer('service') is the same as $zf2App->getServiceManager()->get('service'), you are probably correct that the grabServiceFromContainer should use the $this->application instead of doing an init on the Application again.

Contributor

dranzd commented Nov 6, 2015

If the only purpose of the grabServiceFromContainer('service') is the same as $zf2App->getServiceManager()->get('service'), you are probably correct that the grabServiceFromContainer should use the $this->application instead of doing an init on the Application again.

dranzd pushed a commit to dranzd/Codeception that referenced this issue Nov 7, 2015

rj
Grab service from the current app and not initialize a new one
 * fix issue #2519 where Doctrine2 gets different instance of the
   entity manager everytime ZF2's grabServiceFromContainer method
   is called
@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Nov 8, 2015

Member

@dranzd Please make a pull request.

Member

Naktibalda commented Nov 8, 2015

@dranzd Please make a pull request.

dranzd added a commit to dranzd/Codeception that referenced this issue Nov 9, 2015

Grab service from the current app and not initialize a new one
 * fix issue #2519 where Doctrine2 gets different instance of the
   entity manager everytime ZF2's grabServiceFromContainer method
   is called

@Naktibalda Naktibalda closed this Nov 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment