[PR #420] [CLOSED] Cascade dependent identities to associations #8161

Open
opened 2026-01-22 15:58:42 +01:00 by admin · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/doctrine/orm/pull/420
Author: @djlambert
Created: 8/8/2012
Status: Closed

Base: masterHead: CascadeDependantId


📝 Commits (3)

  • 801c742 When cascading associations check if associated entity's identity is dependant and null, if so set identity.
  • 8ed434b If associated entity is one-to-one and cascade persist check if identifier is dependant and set if null.
  • 1d36dd2 Tests for cascading dependant identities to associations.

📊 Changes

2 files changed (+377 additions, -0 deletions)

View changed files

📝 lib/Doctrine/ORM/UnitOfWork.php (+19 -0)
tests/Doctrine/Tests/ORM/Functional/CascadeDependantIdentityTest.php (+358 -0)

📄 Description

Currently when an entity in a one-to-one association gets its identity from another entity it requires calling the setter for the associated entity from the setter in the identity source entity setter or passing the identity source entity to the associated entity's constructer, something similar to this:

<?php
/**
 * @Entity
 */
class User
{
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;

    /** @OneToOne(targetEntity="Address", mappedBy="user", cascade={"persist"}) */
    private $address;

    public setAddress(Address $address)
    {
        $this->address = $address;
        $address->setUser($this); // Calling setter in associated entity
    }
}

/**
 * @Entity
 */
class Address
{
    /** @Id @OneToOne(targetEntity="User", inversedBy="address") */
    private $user;

    __construct(User $user)
    {
        $this->user = $user; // Passing identity source to constructor
    }

    public setUser(User $user)
    {
        $this->user = $user;
    }
}

$user = new User();
$em->persist($user);
$em->flush();

$address = new Address($user); // If using constructor
$user->setAddress($address);
// OR
$user->setAddress(new Address()); // If using setter

$em->flush();

This PR adds checks for associations with cascade persist that have dependent identities which are empty, and sets the field in the associated entity to the identity source entity. Granted it's not complicated to do as the previous example, but this allows for simpler code. It also seems a logical assumption that when the association is cascade persist this should happen automatically:

<?php
/**
 * @Entity
 */
class User
{
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;

    /** @OneToOne(targetEntity="Address", mappedBy="user", cascade={"persist"}) */
    private $address;

    public setAddress(Address $address)
    {
        $this->address = $address;
    }
}

/**
 * @Entity
 */
class Address
{
    /** @Id @OneToOne(targetEntity="User", inversedBy="address") */
    private $user;

    // This could be removed if not needed
    public setUser(User $user)
    {
        $this->user = $user;
    }
}

$user = new User();
$em->persist($user);
$em->flush();

$user->setAddress(new Address());

$em->flush();

Now only if it wasn't so wordy to explain :)


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/doctrine/orm/pull/420 **Author:** [@djlambert](https://github.com/djlambert) **Created:** 8/8/2012 **Status:** ❌ Closed **Base:** `master` ← **Head:** `CascadeDependantId` --- ### 📝 Commits (3) - [`801c742`](https://github.com/doctrine/orm/commit/801c742c68fe9aef8f513109a8d7bd08ca9946a1) When cascading associations check if associated entity's identity is dependant and null, if so set identity. - [`8ed434b`](https://github.com/doctrine/orm/commit/8ed434bf8c20fb72f984dc8578f56455c02476f6) If associated entity is one-to-one and cascade persist check if identifier is dependant and set if null. - [`1d36dd2`](https://github.com/doctrine/orm/commit/1d36dd2f632319f258ed64861385394c2bacb50f) Tests for cascading dependant identities to associations. ### 📊 Changes **2 files changed** (+377 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `lib/Doctrine/ORM/UnitOfWork.php` (+19 -0) ➕ `tests/Doctrine/Tests/ORM/Functional/CascadeDependantIdentityTest.php` (+358 -0) </details> ### 📄 Description Currently when an entity in a one-to-one association gets its identity from another entity it requires calling the setter for the associated entity from the setter in the identity source entity setter or passing the identity source entity to the associated entity's constructer, something similar to this: ``` php <?php /** * @Entity */ class User { /** @Id @Column(type="integer") @GeneratedValue */ private $id; /** @OneToOne(targetEntity="Address", mappedBy="user", cascade={"persist"}) */ private $address; public setAddress(Address $address) { $this->address = $address; $address->setUser($this); // Calling setter in associated entity } } /** * @Entity */ class Address { /** @Id @OneToOne(targetEntity="User", inversedBy="address") */ private $user; __construct(User $user) { $this->user = $user; // Passing identity source to constructor } public setUser(User $user) { $this->user = $user; } } $user = new User(); $em->persist($user); $em->flush(); $address = new Address($user); // If using constructor $user->setAddress($address); // OR $user->setAddress(new Address()); // If using setter $em->flush(); ``` This PR adds checks for associations with cascade persist that have dependent identities which are empty, and sets the field in the associated entity to the identity source entity. Granted it's not complicated to do as the previous example, but this allows for simpler code. It also seems a logical assumption that when the association is cascade persist this should happen automatically: ``` php <?php /** * @Entity */ class User { /** @Id @Column(type="integer") @GeneratedValue */ private $id; /** @OneToOne(targetEntity="Address", mappedBy="user", cascade={"persist"}) */ private $address; public setAddress(Address $address) { $this->address = $address; } } /** * @Entity */ class Address { /** @Id @OneToOne(targetEntity="User", inversedBy="address") */ private $user; // This could be removed if not needed public setUser(User $user) { $this->user = $user; } } $user = new User(); $em->persist($user); $em->flush(); $user->setAddress(new Address()); $em->flush(); ``` Now only if it wasn't so wordy to explain :) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
admin added the pull-request label 2026-01-22 15:58:42 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#8161