1

We had to refactor many entities in our application because we share database and tables with another app and there was an update that changed the data structure and we must follow up to the new structure.

This meant that some entities were split in 2 or 3 tables so we followed the tutorial for setting up OneToOne relations but ended up with an issue on every attempt to persist both main and related entities.

Whenever we try to flush changes through the entity manager (after persisting both main and related entity either with or without cascade persist) we get 500 error status response, no response data (even using app_dev.php) and we get this message in the log:

request.CRITICAL: 
Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: 
"Notice: Undefined index: 000000003440ddf300000000391d8640" at 
vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 2905 
{"exception":"[object] 
(Symfony\\Component\\Debug\\Exception\\ContextErrorException(code: 0): 
Notice: Undefined index: 000000003440ddf300000000391d8640 at 
vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\UnitOfWork.php:2905)"} []

We are creating new entities so there is not an issue with cached entities.

Here is some sample code on how we defined the entities and the relations. (we tried both with and without id property but we ended up with the same issue)

/**
 * @ORM\Table(name="contacts_accounts_1_c")
 * @ORM\Entity
 */
class ContactsAccounts1C
{
    /**
     * @var string
     * @ORM\Column(name="id", type="string", length=36, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var ContactsAccounts1CExtradata
     * @ORM\OneToOne(targetEntity="ContactsAccounts1CExtradata", mappedBy="principal", cascade={"ALL"})
     */
    private $extradata;
    ...
}

/**
 * @ORM\Table(name="contacts_accounts_1_c_extradata")
 * @ORM\Entity()
 */
class ContactsAccounts1CExtradata {
    /**
     * @var string
     * @ORM\Column(name="id", type="string", length=36, nullable=false)
     */
    private $id;

    /**
     * @var ContactsAccounts1C
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="ContactsAccounts1C", inversedBy="extradata")
     * @ORM\JoinColumn(name="id")
     */
    private $principal;
    ...
}

If we avoid setting/creating the extradata entity the principal entity ends up being successfully persisted.

I found this question that seems to be related but still not solved.

Thanks in advance for your help.

Community
  • 1
  • 1
Jev
  • 11
  • 1

2 Answers2

0

At a quick glance, this annotation looks out of place on principal:

 * @ORM\JoinColumn(name="id")

Given that your relation is principal -> extradata it seems to have no place there.

Richard
  • 4,079
  • 1
  • 14
  • 17
  • Yes since it's a join through the primary key it's not needed but it's there as a reminder. – Jev Feb 23 '16 at 10:10
0

Found the issue: GeneratedValue was being wrongly defined and overrided for the main entity. And the solution: Use @GeneratedValue(strategy="UUID") for principal entity (owning side) and @GeneratedValue(strategy="NONE") for related entities (owned side).

Jev
  • 11
  • 1