Clone entity in CASCADE mode

2019-06-08 14:40发布

I need to find a object in DB by some parameters, lets take ID as example then I have this entity:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AppBundle\Model\IdentifierAutogeneratedEntityTrait;
use DateTime;

/**
 * @ORM\Entity
 * @ORM\Table(name="negocio.solicitud_usuario", schema="negocio")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\SolicitudUsuarioRepository")
 */
class SolicitudUsuario
{
    use IdentifierAutogeneratedEntityTrait;

    /**
     * @ORM\ManyToOne(targetEntity="SolicitudUsuario", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="padre_id", referencedColumnName="id")
     */
    protected $padre;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="ProductoSolicitud", mappedBy="solicitud_usuario")
     */
    protected $producto_solicitud;

    /**
    * @var ArrayCollection
    * @ORM\OneToMany(targetEntity="Constancia", mappedBy="solicitud_usuario")
    */
    protected $constancias;


    /**
     * @ORM\ManyToOne(targetEntity="Usuario", cascade={"persist"})
     * @ORM\JoinColumn(name="usuario_id", referencedColumnName="id")
     */
    protected $usuario;

    /**
     * @ORM\ManyToOne(targetEntity="TipoTramite", cascade={"persist"})
     * @ORM\JoinColumn(name="tipo_tramite_id", referencedColumnName="id")
     */
    protected $tipo_tramite;

    /**
     * @ORM\Column(name="fecha_creacion", type="datetime", nullable=false)
     */
    protected $fecha_creacion;

    /**
     * @ORM\ManyToOne(targetEntity="TipoRegistro", cascade={"persist"})
     * @ORM\JoinColumn(name="tipo_registro_id", referencedColumnName="id")
     */
    protected $tipo_registro;

    /**
     * @ORM\ManyToOne(targetEntity="EstadoSolicitud", cascade={"persist"})
     * @ORM\JoinColumn(name="estado_solicitud_id", referencedColumnName="id")
     */
    protected $estado_solicitud;

    /**
     * @ORM\Column(name="diferencia_pago", type="decimal", precision=6, scale=2, nullable=false)
     */
    protected $diferencia_pago;

    /**
     * @ORM\ManyToOne(targetEntity="OficinaRegional", cascade={"persist"})
     * @ORM\JoinColumn(name="oficina_regional_id", referencedColumnName="id", nullable=true)
     */
    protected $oficina_regional;   
}

That entity has a lot of relations as you may already notice and also the related entities has other relations and so on. How, finding just SolicitudUsuario I clone the current record|object keeping integrity? Take a look to this example:

  • Find SolicitudUsuario by ID=1
  • Record found
    • Clone current SolicitudUSuario entity and all it's related entities to a new one.
    • The new record will have padre=1

Any advice? It's clear what I want to achieve?

0条回答
登录 后发表回答