[ContextErrorException] doctrine generate entities with embedded fields #5016

Closed
opened 2026-01-22 14:56:36 +01:00 by admin · 14 comments
Owner

Originally created by @yceruto on GitHub (Feb 11, 2016).

Originally assigned to: @Majkl578 on GitHub.

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.

Originally created by @yceruto on GitHub (Feb 11, 2016). Originally assigned to: @Majkl578 on GitHub. I've an entity with one embedded field: ``` php 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](https://github.com/doctrine/doctrine2/blob/2.5/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L3252-L3253)): ``` diff - $this->reflClass->name, - $embeddable->reflClass->name, + $this->name, + $embeddable->name, ``` if they agree I can create a PR to fix this.
admin added the BugWon't Fix labels 2026-01-22 14:56:36 +01:00
admin closed this issue 2026-01-22 14:56:36 +01:00
Author
Owner

@Ocramius commented on GitHub (Feb 11, 2016):

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.

@Ocramius commented on GitHub (Feb 11, 2016): 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](https://github.com/doctrine/common/blob/773c56e51fc26ae07a47146fae31b320dd6cada3/lib/Doctrine/Common/Persistence/Mapping/StaticReflectionService.php#L65).
Author
Owner

@yceruto commented on GitHub (Feb 11, 2016):

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

@yceruto commented on GitHub (Feb 11, 2016): Exactly, in this case the reflection service class is `StaticReflectionService`, then `$this->reflClass` is `null`. What would be the right solution?
Author
Owner

@Ocramius commented on GitHub (Feb 11, 2016):

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

@Ocramius commented on GitHub (Feb 11, 2016): Wondering if populating the reflection with a fake reflection instance could work. This reflection service has bitten us many many times...
Author
Owner

@yceruto commented on GitHub (Feb 11, 2016):

@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.

@yceruto commented on GitHub (Feb 11, 2016): @Ocramius I've another issue related with the same command `doctrine:generate:entities` when exists more than one levels embedded, I found this [commit](https://github.com/doctrine/doctrine2/commit/9097014c3d68f50632075f7f1d62400bf8f0f817) with a solution but I don't see it in 2.5 or master branchs.
Author
Owner

@Ocramius commented on GitHub (Feb 11, 2016):

That commit is in a fork, no?

@Ocramius commented on GitHub (Feb 11, 2016): ~~That commit is in a fork, no?~~
Author
Owner

@Ocramius commented on GitHub (Feb 11, 2016):

No, never mind. It is in 2.5.

@Ocramius commented on GitHub (Feb 11, 2016): No, never mind. [It is in 2.5](https://github.com/doctrine/doctrine2/blob/788143dc0313c7522820ebf6057f73881e7190a3/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php#L1130).
Author
Owner

@yceruto commented on GitHub (Feb 11, 2016):

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

@yceruto commented on GitHub (Feb 11, 2016): The exception is thrown [here](https://github.com/doctrine/doctrine2/blob/9097014c3d68f50632075f7f1d62400bf8f0f817/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L932), because [here](https://github.com/doctrine/doctrine2/blob/9097014c3d68f50632075f7f1d62400bf8f0f817/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L927) `$parentReflFields` it's empty on first occurrence, I don't see the solucion [in your tree reference](https://github.com/doctrine/doctrine2/blob/788143dc0313c7522820ebf6057f73881e7190a3/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L931) ??
Author
Owner

@Adam314 commented on GitHub (May 6, 2016):

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 commented on GitHub (May 6, 2016): I had the same error and found solution. If I provide only class name: ``` php /** * @var Price * * @ORM\Embedded(class="Price") */ private $price; ``` I get this error when running `doctrine:generate:entities`. However when I add `columnPrefix`: ``` php /** * @var Price * * @ORM\Embedded(class="Price", columnPrefix="price") */ private $price; ``` then it works. :)
Author
Owner

@leevigraham commented on GitHub (May 18, 2016):

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

@leevigraham commented on GitHub (May 18, 2016): @Adam314 adding columnPrefix seems to work for one level of embeddable but not multiple.
Author
Owner

@binarious commented on GitHub (Sep 27, 2016):

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.

@binarious commented on GitHub (Sep 27, 2016): I had to change https://github.com/doctrine/doctrine2/blob/v2.5.5/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L930 to ``` php if (isset($embeddedClass['declaredField']) && $parentReflFields[$embeddedClass['declaredField']]) { ``` to get this working.
Author
Owner

@soullivaneuh commented on GitHub (Dec 8, 2016):

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.

@soullivaneuh commented on GitHub (Dec 8, 2016): Same issue here but adding `columnPrefix` does not solve the problem for my case: ```php class CreditNote { /** * @var BillingRecipient * * @ORM\Embedded(class="AppBundle\Embeddable\BillingRecipient", columnPrefix="recipient") */ private $recipient; } ``` Installed packages: ```bash $ 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.
Author
Owner

@lashus commented on GitHub (Jan 23, 2017):

+1 still not solved in 2017 ^^

@lashus commented on GitHub (Jan 23, 2017): +1 still not solved in 2017 ^^
Author
Owner

@francisw commented on GitHub (Feb 16, 2017):

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

@francisw commented on GitHub (Feb 16, 2017): Lovely workaround @Adam314 the solution (including the columnPrefix) works in 3.2 for one level of embedding
Author
Owner

@Majkl578 commented on GitHub (Dec 20, 2017):

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

@Majkl578 commented on GitHub (Dec 20, 2017): EntityGenerator is going to be removed in Doctrine 3.0, sorry that it never got solved.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5016