Facing "Undefined index: (fieldname) in Doctrine/ODM/MongoDB/UnitOfWork.php line 736" #433

Closed
vmattila opened this Issue Oct 17, 2012 · 2 comments

Comments

Projects
None yet
3 participants
Contributor

vmattila commented Oct 17, 2012

Hi,

I have recently faced very strange error when trying to update my documents with 1.0.0-beta6 version. The common code for all my trials is as follows:

$dm = $this->get('doctrine_mongodb.odm.default_document_manager');
$session = $dm->getRepository('BrowserSession')->find($this->get('session')->getId());
$session->incrementRequests();

// Continues with one of the code snippets below
  1. If I try to call $dm->persist($session); $dm->flush(); an exception "Undefined index: requests in Doctrine/ODM/MongoDB/UnitOfWork.php line 736" is thrown.

  2. $dm->flush($session); works properly.

  3. $dm->flush(); does not do anything.

I am slightly confused what's going on. Previously, code snippet 1 has worked properly.

The document:

/**
 * @MongoDB\Document()
 * @MongoDB\ChangeTrackingPolicy("DEFERRED_EXPLICIT") 
 */
class BrowserSession
{

    // ID and other fields are here

    /**
     * @MongoDB\Increment
     * @var integer
     */
    protected $requests = 0;

    public function incrementRequests() { $this->requests++; }
}
Owner

jmikola commented Feb 27, 2013

This may be related to #483 (assuming the field name, and not type, is the problem in that issue).

Member

malarzm commented Jun 8, 2015

As expected this is working correctly in latest ODM, here's passing test case :)

<?php

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

class GH433Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
    public function testTest()
    {
        $session = new BrowserSession();
        $this->dm->persist($session);
        $this->dm->flush();
        $this->dm->clear();

        $session = $this->dm->getRepository(get_class($session))->find($session->id);
        $session->incrementRequests();
        $this->dm->persist($session);
        $this->dm->flush();
        $this->dm->clear();

        $session = $this->dm->getRepository(get_class($session))->find($session->id);
        $this->assertEquals(1, $session->requests);
    }
}

/**
 * @MongoDB\Document()
 * @MongoDB\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 */
class BrowserSession
{
    /** @MongoDB\Id */
    public $id;

    /**
     * @MongoDB\Increment
     * @var integer
     */
    public $requests = 0;

    public function incrementRequests() { $this->requests++; }
}

malarzm closed this Jun 8, 2015

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