Orm: [ContextErrorException] doctrine generate entities with embedded fields

Created on 11 Feb 2016  路  14Comments  路  Source: doctrine/orm

I've an entity with one embedded field:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="box")
 * @ORM\Entity()
 */
class Box
{
    //...

    /**
     * @var Payment
     *
     * @ORM\Embedded(class="AppBundle\Entity\Payment")
     */
    private $payment;

    //...
}

Running the command: doctrine:generate:entities AppBundle:Box -vvv throws an exception:

[2016-02-11 14:36:29] event.DEBUG: Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
[2016-02-11 14:36:29] event.DEBUG: Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".
[2016-02-11 14:36:29] event.DEBUG: Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".
Generating entity "AppBundle\Entity\Box"


  [Symfony\Component\Debug\Exception\ContextErrorException]
  Notice: Trying to get property of non-object


Exception trace:
 () at E:\xampp\htdocs\canguro\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataInfo.php:3253
 Symfony\Component\Debug\ErrorHandler->handleError() at E:\xampp\htdocs\canguro\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataInfo.php:3253
 Doctrine\ORM\Mapping\ClassMetadataInfo->inlineEmbeddable() at E:\xampp\htdocs\canguro\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php:200
 Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() at E:\xampp\htdocs\canguro\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php:332
 Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() at E:\xampp\htdocs\canguro\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php:78
 Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() at E:\xampp\htdocs\canguro\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php:225
 Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() at E:\xampp\htdocs\canguro\vendor\doctrine\doctrine-bundle\Mapping\DisconnectedMetadataFactory.php:193
 Doctrine\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory->getMetadataForClass() at E:\xampp\htdocs\canguro\vendor\doctrine\doctrine-bundle\Mapping\DisconnectedMetadataFactory.php:79
 Doctrine\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory->getClassMetadata() at E:\xampp\htdocs\canguro\vendor\doctrine\doctrine-bundle\Command\GenerateEntitiesDoctrineCommand.php:106
 Doctrine\Bundle\DoctrineBundle\Command\GenerateEntitiesDoctrineCommand->execute() at E:\xampp\htdocs\canguro\vendor\symfony\symfony\src\Symfony\Component\Console\Command\Command.php:259
 Symfony\Component\Console\Command\Command->run() at E:\xampp\htdocs\canguro\vendor\symfony\symfony\src\Symfony\Component\Console\Application.php:860
 Symfony\Component\Console\Application->doRunCommand() at E:\xampp\htdocs\canguro\vendor\symfony\symfony\src\Symfony\Component\Console\Application.php:192
 Symfony\Component\Console\Application->doRun() at E:\xampp\htdocs\canguro\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.php:98
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at E:\xampp\htdocs\canguro\vendor\symfony\symfony\src\Symfony\Component\Console\Application.php:123
 Symfony\Component\Console\Application->run() at E:\xampp\htdocs\canguro\app\console:29

I've changed this lines and work for me (source):

- $this->reflClass->name,
- $embeddable->reflClass->name,
+ $this->name,
+ $embeddable->name,

if they agree I can create a PR to fix this.

Bug Won't Fix

Most helpful comment

I had the same error and found solution. If I provide only class name:

/**
 * @var Price
 *
 * @ORM\Embedded(class="Price")
 */
private $price;

I get this error when running doctrine:generate:entities. However when I add columnPrefix:

/**
 * @var Price
 *
 * @ORM\Embedded(class="Price", columnPrefix="price")
 */
private $price;

then it works. :)

All 14 comments

The change seems wrong to me: an entity name is not necessarily its reflection class name in all cases. Wondering why the reflection wasn't initialized in that case, but it seems like the doctrine bundle is using a "cold" reflection service.

Exactly, in this case the reflection service class is StaticReflectionService, then $this->reflClass is null. What would be the right solution?

Wondering if populating the reflection with a fake reflection instance could work. This reflection service has bitten us many many times...

@Ocramius I've another issue related with the same command doctrine:generate:entities
when exists more than one levels embedded, I found this commit with a solution but I don't see it in 2.5 or master branchs.

That commit is in a fork, no?

No, never mind. It is in 2.5.

The exception is thrown here, because here $parentReflFields it's empty on first occurrence, I don't see the solucion in your tree reference ??

I had the same error and found solution. If I provide only class name:

/**
 * @var Price
 *
 * @ORM\Embedded(class="Price")
 */
private $price;

I get this error when running doctrine:generate:entities. However when I add columnPrefix:

/**
 * @var Price
 *
 * @ORM\Embedded(class="Price", columnPrefix="price")
 */
private $price;

then it works. :)

@Adam314 adding columnPrefix seems to work for one level of embeddable but not multiple.

I had to change https://github.com/doctrine/doctrine2/blob/v2.5.5/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L930
to

if (isset($embeddedClass['declaredField']) && $parentReflFields[$embeddedClass['declaredField']]) {

to get this working.

Same issue here but adding columnPrefix does not solve the problem for my case:

class CreditNote
{
    /**
     * @var BillingRecipient
     *
     * @ORM\Embedded(class="AppBundle\Embeddable\BillingRecipient", columnPrefix="recipient")
     */
    private $recipient;
}

Installed packages:

$ composer show doctrine/*
doctrine/annotations                v1.3.0 Docblock Annotations Parser
doctrine/cache                      v1.6.1 Caching library offering an object-oriented API for many cache backends
doctrine/collections                v1.3.0 Collections Abstraction library
doctrine/common                     v2.6.2 Common Library for Doctrine projects
doctrine/data-fixtures              v1.2.2 Data Fixtures for all Doctrine Object Managers
doctrine/dbal                       v2.5.5 Database Abstraction Layer
doctrine/doctrine-bundle            1.6.4  Symfony DoctrineBundle
doctrine/doctrine-cache-bundle      1.3.0  Symfony Bundle for Doctrine Cache
doctrine/doctrine-fixtures-bundle   2.3.0  Symfony DoctrineFixturesBundle
doctrine/doctrine-migrations-bundle v1.2.0 Symfony DoctrineMigrationsBundle
doctrine/inflector                  v1.1.0 Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator               1.0.5  A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                      v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations                 1.4.1  Database Schema migrations using Doctrine DBAL
doctrine/orm                        v2.5.5 Object-Relational-Mapper for PHP

Removing the embedded field makes the command work.

+1 still not solved in 2017 ^^

Lovely workaround @Adam314 the solution (including the columnPrefix) works in 3.2 for one level of embedding

EntityGenerator is going to be removed in Doctrine 3.0, sorry that it never got solved.

Was this page helpful?
0 / 5 - 0 ratings