简体   繁体   English

无法使用symfony2将onetoone转换为学说中的onetomany

[英]Cant convert onetoone to onetomany in doctrine using symfony2

I got hired about 2 weeks in a company, and my first task was to update and implement new features in an existing software written in symfony2-doctrine. 我在一家公司工作了大约2个星期,我的第一个任务是在以symfony2-doctrine编写的现有软件中更新和实现新功能。 But one of the changes I must make is just breaking my back. 但是我必须做出的改变之一只是折腾我。

The previous model was: one "cliente" (costumer) could have only one "credito" (credit) and one "credito" could have many "venta" (sales) The new model should be: one "cliente" (costumer) can have many "credito" and one "credito" can have many "venta" (Im keeping the onetomany association for backwards compatibity) 以前的模型是:一个“客户”(客户)可以只有一个“信用”(信贷),一个“信贷”中可以有很多“ venta”(销售)新模型应该是:一个“客户”(客户)可以有很多“ credito”,而一个“ credito”可以有许多“ venta”(我保持了onetomany关联以实现向后兼容性)

This is how my entities look like this: 这是我的实体的样子:

cliente.php cliente.php

class Cliente {
//put your code here
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

 /**   
 * @ORM\Column(type="string")
 * @Assert\NotBlank()
 */
protected $nombre;

/**   
 * @ORM\Column(type="string", unique=true)
 * @Assert\NotBlank()
 */
protected $documento;


/**   
 * @ORM\Column(type="string", nullable=true)    
 */
protected $direccion;

/**   
 * @ORM\Column(type="string", nullable=true)     
 */
protected $telefono;

/**   
 * @ORM\Column(type="string", nullable=true)     
 */
protected $celular;

/**   
 * @ORM\Column(type="string", nullable=true)
 * @Assert\Email()
 */
protected $email;

/**   
 * @ORM\Column(type="string", nullable=true)

 */
protected $ciudad;


/**   
 * @ORM\Column(type="string", nullable=true)

 */
protected $departamento;

/**   
 * @ORM\Column(type="string", nullable=true)

 */
protected $referenciaFamiliar;

/**   
 * @ORM\Column(type="string", nullable=true)

 */
protected $referenciaFamiliarTelefono;

/**   
 * @ORM\Column(type="string", nullable=true)

 */
protected $referenciaPersonal;


/**   
 * @ORM\Column(type="string", nullable=true)

 */
protected $referenciaPersonalTelefono;


/**   
 * @ORM\OneToMany(targetEntity="Credito", mappedBy="cliente", cascade={"all"})
 */
protected $credito;



/**   
 * @ORM\Column(type="string", nullable=true, length=255)     
 */
protected $photo;

/**
 * @Assert\File(maxSize="300k", mimeTypes={"image/jpeg","image/png"},mimeTypesMessage="Debe subir una imagen JPG o PNG",maxSizeMessage="La imagen no puede pesar más de 300 kb.")
 */

protected $file;

credito.php credito.php

class Credito {
//put your code here
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

/**   
 * @ORM\Column(type="string",nullable=true)
 * @Assert\NotBlank()
 */
protected $cc;

/**   
 * @ORM\Column(type="datetime",nullable=false)
 * @Assert\DateTime()
 */
protected $fechaRegistro;

/**
 * @ORM\ManyToOne(targetEntity="Cliente",cascade={"persist"})
 * @ORM\JoinColumn(name="cliente_id",referencedColumnName="id")     
 */
protected $cliente;

/**
 * @ORM\OneToMany(targetEntity="Venta", mappedBy="credito",cascade={"all"})         
 */
protected $ventas;

/**   
 * @ORM\OneToMany(targetEntity="Abono", mappedBy="credito",cascade={"persist"})
 */
protected $abonos;

/**   
 * @ORM\Column(type="datetime",nullable=true)
 * @Assert\DateTime()
 */
protected $fechaProximoPago;

/**   
 * @ORM\Column(type="decimal",scale=3, precision=23)
 * @Assert\NotBlank()
 */
protected $valorProximoPago;

/**   
 * @ORM\Column(type="integer")
 * @Assert\NotBlank()
 */
protected $cuotasTotales;

/**   
 * @ORM\Column(type="integer")
 * @Assert\NotBlank()
 */
protected $cuotasPagadas;



/**
 * @ORM\ManyToOne(targetEntity="ModoPagoNom")
 * @ORM\JoinColumn(name="modo_pago_id",referencedColumnName="id")  

 */
protected $modoPago;

/**   
 * @ORM\Column(type="decimal",scale=3, precision=23)
 * @Assert\NotBlank()
 */
protected $valorFinanciado;


/**   
 * @ORM\Column(type="decimal",scale=3, precision=23)
 * @Assert\NotBlank()
 */
protected $cupo;


/**   
 * @ORM\Column(type="decimal",scale=3, precision=23)
 * @Assert\NotBlank()
 */
protected $cupoUsado;

venta.php venta.php

class Venta{
//put your code here
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

 /**   
 * @ORM\Column(type="datetime")
 * @Assert\DateTime()
 */
protected $fecha;

/**   
 * @ORM\Column(type="datetime")
 * @Assert\DateTime()
 */
protected $fechaPrimerPago;

/**   
 * @ORM\Column(type="string")
 * @Assert\NotBlank()
 */
protected $numeroFactura;

/**   
 * @ORM\Column(type="string")
 * @Assert\NotBlank()
 */
protected $numeroAutorizo;




/**   
 * @ORM\Column(type="decimal",scale=3, precision=23)
 * @Assert\Min(limit="0", message="El valor de la factura debe ser positivo")
 */
protected $valorFactura;

/**   
 * @ORM\Column(type="integer")
 * @Assert\Type("integer")
 * @Assert\Min(limit="1", message="Debe especificar al menos una cuota")
 */
protected $numeroCuotas;


/**   
 * @ORM\Column(type="integer")
 * @Assert\Min(0)
 */
protected $cuotasPagadas;

/**
 * @ORM\ManyToOne(targetEntity="ModoPagoNom")
 * @ORM\JoinColumn(name="modo_pago_id",referencedColumnName="id")  

 */
protected $modoPago;

/**
 * @ORM\ManyToOne(targetEntity="Credito",cascade={"persist"})
 * @ORM\JoinColumn(name="credito_id",referencedColumnName="id")     
 */
protected $credito;


/**   
 * @ORM\Column(type="decimal",scale=3, precision=23)
 * @Assert\Min(0.0)
 */
protected $valorFinanciado;

/**   
 * @ORM\Column(type="decimal",scale=3, precision=23)
 * @Assert\Min(0.0)
 */
protected $valorCuota;    

/**   
 * @ORM\Column(type="decimal",scale=3, precision=23)
 * @Assert\Min(0.0)
 */
protected $valorPrimeraCuota; 

/**
 * @ORM\ManyToOne(targetEntity="Almacen")
 * @ORM\JoinColumn(name="almacen_id",referencedColumnName="id")        
 */
protected $almacen;

/**
 * Get id
 *
 * @return integer 
 */

The problem is: everytime I insert a new "credito", it wont be linked to the existing "cliente" instead of that, it will try to insert a duplicate entry of "cliente". 问题是:每次我插入一个新的“ credito”时,它都不会链接到现有的“ cliente”,而是会尝试插入一个重复的“ cliente”条目。 I've tried many things but none of them had any effect. 我已经尝试了很多东西,但是没有任何作用。

I appreciate any help, cause Im stuck with that. 非常感谢您的帮助,因为我一直坚持下去。 If further information or code is required I will gladly provide it. 如果需要更多信息或代码,我们将很乐意提供。

The cascade={"persist"} should be the core of your problem: cascade={"persist"}应该是您问题的核心:

class Credito {
......
/**
 * @ORM\ManyToOne(targetEntity="Cliente",cascade={"persist"})
 * @ORM\JoinColumn(name="cliente_id",referencedColumnName="id")     
 */
protected $cliente;

It tells doctrine to persist cleinte always when you persist Credito. 它告诉您在坚持Credito时始终坚持cleinte。 See docs for more details http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations 有关更多详细信息,请参阅文档http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations

Always tell Doctrine how to do in both sides of an association : 始终告诉Doctrine在关联的两边该怎么做:

// credito.php
/**
 * @ORM\ManyToOne(targetEntity="Cliente", inversedBy="credito", cascade={"persist"})
 * @ORM\JoinColumn(name="cliente_id",referencedColumnName="id")     
 */
protected $cliente;

And

// venta.php
/**
 * @ORM\ManyToOne(targetEntity="Credito", inversedBy="ventas", cascade={"persist"})
 * @ORM\JoinColumn(name="credito_id",referencedColumnName="id")     
 */
protected $credito;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM