0

On Doctrine's documentation page that describes how to implement __clone, the following logic is used:

<?php
class MyEntity
{
    private $id; // This is the identifier of the entity.
    //...

    public function __clone()
    {
        // If the entity has an identity, proceed as normal.
        if ($this->id) {
            // ... Your code here as normal ...
        }
        // otherwise do nothing, do NOT throw an exception!
    }

    //...
}

... and the following explanation is given (bold added by me):

These implementations are possible and safe because when Doctrine invokes these methods, the entities never have an identity (yet). Furthermore, it is possibly a good idea to check for the identity in your code anyway, since it's rarely the case that you want to unserialize or clone an entity with no identity.

Doesn't this mean that the branch titled Your code here as normal will absolutely never be reached and is therefor useless? What am I missing here?

  • The documentation has no example and is poor. – Cid Sep 06 '19 at 07:53
  • Although you are not asking for related entities like in the duplicate, the accepted answer shows an example on how to implement correctly `clone` – Cid Sep 06 '19 at 07:55
  • [this](https://stackoverflow.com/questions/9071094/how-to-re-save-the-entity-as-another-row-in-doctrine-2) is related too – Cid Sep 06 '19 at 07:58

0 Answers0