Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot persist parent entity if OneToOne shares the same id #6531

Closed
davidbarratt opened this issue Jun 27, 2017 · 2 comments
Closed

Cannot persist parent entity if OneToOne shares the same id #6531

davidbarratt opened this issue Jun 27, 2017 · 2 comments
Assignees
Labels
Milestone

Comments

@davidbarratt
Copy link

davidbarratt commented Jun 27, 2017

If you have an Entity like this:

/**
 * @ORM\Entity
 * @ORM\Table(name="takedown")
 */
class Takedown {

	/**
	 * @var int
	 *
	 * @ORM\Column(name="takedown_id", type="integer")
	 * @ORM\Id
	 * @ORM\GeneratedValue
	 */
	private $id;

	/**
	 * @var DigitalMillenniumCopyrightAct
	 *
	 * @ORM\OneToOne(targetEntity="DigitalMillenniumCopyrightAct", mappedBy="takedown", orphanRemoval=true, cascade={"persist"})
	 */
	private $dmca;

	public function setDmca( $dmca = null ) : self {
		if ( $dmca === null ) {
			$this->dmca = null;

			return $this;
		}

		$this->dmca = $dmca;
		$this->dmca->setTakedown( $this );

		return $this;
	}

	public function getDmca() :? DigitalMillenniumCopyrightAct {
		return $this->dmca;
	}
}

and the related entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="takedown_dmca")
 */
class DigitalMillenniumCopyrightAct {

	/**
	 * @ORM\Id
	 * @ORM\OneToOne(
	 *	targetEntity="Takedown",
	 *	inversedBy="dmca"
	 *)
         * @ORM\JoinColumn(name="takedown_id", referencedColumnName="takedown_id")
	 */
	private $takedown;

	public function setTakedown( Takedown $takedown ) : self {
		$this->takedown = $takedown;

		return $this;
	}

	public function getTakedown( ) :? Takedown {
		return $this->takedown;
	}
}

Doing something like this:

$takedown = new Takedown();
$takedown->setDmca(new DigitalMillenniumCopyrightAct());

$em->persist($takedown);
$em->flush();

will always fail with an error:

The given entity of type 'DigitalMillenniumCopyrightAct' (DigitalMillenniumCopyrightAct@000000000c1a46f70000000005683920) has no identity/no id values set. It cannot be added to the identity map.

I've also tried explicit persisting:

$takedown = new Takedown();
$takedown->setDmca(new DigitalMillenniumCopyrightAct());

$em->persist($takedown);
$em->persist($takedown->getDmca());
$em->flush();

But also fails with the same error.

The only work-around I have found is to remove the related entity completely, then persist & flush and then persist the related entity:

$takedown = new Takedown();
$takedown->setDmca(new DigitalMillenniumCopyrightAct());

$dmca = $takedown->getDmca();
$takedown->setDmca();

$em->persist($takedown);
$em->flush();

$takedown->setDmca($dmca);
$em->persist($takedown->getDmca());
$em->flush():

Is there a way to do this without removing the related entities, flushing, and then persisting the related entities afterwards? I think it would be a lot easier if it was possible, if not, is there some way to make it part of the same transaction? (so if the related entity fails persisting, the initial one will be rolled back?)

@lcobucci
Copy link
Member

@davidbarratt this seems to be related to the commit order calculator that was refactored on 2.6 and couldn't be backported, could you please send us a failing test case that reproduces that behaviour (using master)? It would help us a lot to identify and fix the issue you're describing.

You can find examples on https://github.com/doctrine/doctrine2/tree/388afb46d0cb3ed0c51332e8df0de9e942c2690b/tests/Doctrine/Tests/ORM/Functional/Ticket

@grazz
Copy link

grazz commented Aug 11, 2017

Was this ever addressed? Do you still need a code sample of a use case?

@guilhermeblanco guilhermeblanco self-assigned this Dec 19, 2017
@lcobucci lcobucci added this to the 2.6.1 milestone Feb 19, 2018
@lcobucci lcobucci added the Bug label Feb 19, 2018
@lcobucci lcobucci assigned lcobucci and unassigned guilhermeblanco Feb 19, 2018
lcobucci added a commit that referenced this issue Feb 19, 2018
Add failing tests for #6531 

Fixes #6043
Fixes #6531
Fixes #7002
Fixes #7003
lcobucci pushed a commit to cb8/doctrine2 that referenced this issue Feb 25, 2018
lcobucci pushed a commit to cb8/doctrine2 that referenced this issue Feb 25, 2018
maglnet pushed a commit to maglnet/doctrine2 that referenced this issue Oct 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants