我在一家公司工作了大约两周,我的第一个任务是在用 symfony2-doctrine 编写的现有软件中更新和实现新功能。但我必须做出的改变之一就是让我的背部受伤。
之前的模型是:一个“cliente”(客户)只能有一个“credito”(信用),一个“credito”可以有多个“venta”(销售) 新的模型应该是:一个“cliente”(客户)可以有多个“credito”,一个“credito”可以有多个“venta”(我保留 onetomany 关联以实现向后兼容性)
这就是我的实体的样子:
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
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;
文塔.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
*/
问题是:每次我插入新的“credito”时,它都不会链接到现有的“cliente”,而是会尝试插入“cliente”的重复条目。 我尝试了很多方法,但没有任何效果。
我感谢任何帮助,因为我坚持这一点。 如果需要更多信息或代码,我很乐意提供。
最佳答案
cascade={"persist"}
应该是您问题的核心:
class Credito {
......
/**
* @ORM\ManyToOne(targetEntity="Cliente",cascade={"persist"})
* @ORM\JoinColumn(name="cliente_id",referencedColumnName="id")
*/
protected $cliente;
它告诉原则,当你坚持 Credito 时,始终坚持 cleinte。 有关更多详细信息,请参阅文档 http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations
关于php - 无法使用 symfony2 将 onetoone 转换为 onetomany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23552482/