1

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?

ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Maybe this question will be usefull: http://stackoverflow.com/questions/7533695/doctrine-2-how-to-clone-all-values-from-one-object-onto-another-except-id – chapay Feb 21 '15 at 19:35
  • You want to clone this object and clone all his related objects? Is this the question? – DonCallisto Feb 23 '15 at 11:40
  • @DonCallisto yes, that's exactly what I want to achieve and I think to write at `__clone()` in entity but has not idea in how to, could you point me on the right direction, perhaps some example code? – ReynierPM Feb 23 '15 at 11:55
  • http://stackoverflow.com/questions/14158111/deep-clone-doctrine-entity-with-related-entities – DonCallisto Feb 23 '15 at 12:55
  • @DonCallisto I'm thinking to use [DeepCopy](https://github.com/myclabs/DeepCopy) as suggested [here](http://stackoverflow.com/a/26928339/719427) now lets said that I've entities A, B and C, A hasMany B and B hasMany C but C can have many B also, how should my code looks like? Also I want to clone A but keeping the same information in cloned B and C meaning the only I want to update is the reference to the cloned A, is that what DeepClone does? Any advice? Could you provide an answer explaining this? – ReynierPM Feb 26 '15 at 01:26

0 Answers0