Skriptas
Skriptas

Reputation: 889

Symfony MongoDb can't retrieve new field values

I have added new entry in Document description

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

Then let Doctrine generate entities. Now newly created records have new field "city" with values as expected. However I can see these values only in mongo console. In Doctrine output they are allways set to "null". The entity entries seems correct

public function getFirstName()
{
    return $this->firstName;
}

/**
 * Get city
 *
 * @return string $city
 */
public function getCity()
{
    return $this->city;
}

I have repository

public function allQuery($cat)
{
    $q = $this->createQueryBuilder()
        ->sort('createdAt', 'DESC');

    if ($cat) {
        $q->field('category.$id')->equals(new \MongoId($cat));
    }
    return $q;
}

And service

function addAllPager($perPage = 10, $cat)
{
    return $this->_addPager($this->repo()->allQuery($cat), $perPage);
}

In Controller

    $helper = $this->get('appbundle.test.helper');
    $tests = $helper->addAllPager(10, $cat);

Symfony profiler shows me query db.Test.find().sort({ "createdAt": -1 }).limit(10).skip(0). Dumped Contents of $tests

#firstName: "John"
#city: null

What I am missing?

EDIT

Cache clearing with php bin/console cache:clear solved the problem. php bin/console doctrine:mongodb:cache:clear-metadata was not enough. Thank you malarzm.

Upvotes: 2

Views: 711

Answers (1)

Andre Dixon
Andre Dixon

Reputation: 360

I know this is 8 months after the question has been asked but had the same issue and fought with doctrine for a while. I am using Symfony 3 and I tried php bin/console doctrine:mongodb:cache:clear-metadata with no luck.

I finally ran the command php bin/console cache:clear or just delete the cache with this command sudo rm -rf var/cache and that fixed the issue.

Upvotes: 1

Related Questions