Symfony 我能';t使用实体管理器清除循环外部的相关对象
我试图从Caja对象中删除Libreta的相关对象,但是当我在foreach代码之外使用flush()时,删除不起作用,而在其他代码或示例中,我可以在代码之外使用flush做同样的事情。为什么? 这是代码中起作用的部分 工作示例Symfony 我能';t使用实体管理器清除循环外部的相关对象,symfony,doctrine-orm,Symfony,Doctrine Orm,我试图从Caja对象中删除Libreta的相关对象,但是当我在foreach代码之外使用flush()时,删除不起作用,而在其他代码或示例中,我可以在代码之外使用flush做同样的事情。为什么? 这是代码中起作用的部分 工作示例 $formulario->handleRequest($peticion); if ($formulario->isValid()) { if($formulario->get('regresar')->
$formulario->handleRequest($peticion);
if ($formulario->isValid()) {
if($formulario->get('regresar')->isClicked()){
return $this->redirect($this->generateUrl('super_admin_main_caja'));
}
if($formulario->get('guardar')->isClicked()){
$caja = $formulario->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($caja);
$em->flush();
return $this->redirect($this->generateUrl('super_admin_update_caja', array('cajaId' => $caja->getId())));
}
if($formulario->get('deshabilitar_caja')->isClicked()){
$caja = $formulario->getData();
$em = $this->getDoctrine()->getManager();
foreach ($caja->getLibretas() as $libreta) {
$libreta->setCaja(NULL);
$em->remove($libreta);
$em->flush();
}
$caja->setEstado('DESHABILITADO');
$em->persist($caja);
$em->flush();
return $this->redirect($this->generateUrl('super_admin_main_caja'));
}
但是,当我尝试相同的代码,但与此修改,我不能删除相关的对象,我没有得到任何错误
foreach ($caja->getLibretas() as $libreta) {
$libreta->setCaja(NULL);
$em->remove($libreta);
}
$caja->setEstado('DESHABILITADO');
$em->persist($caja);
$em->flush();
Caja.php
<?php
namespace PD\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use APY\DataGridBundle\Grid\Mapping as GRID;
/**
*
* @ORM\Entity
* @ORM\Table(name="caja")
*
*/
class Caja
{
/**
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
* @GRID\Column(title="Número carton")
*/
protected $numero_carton;
/** @ORM\Column(type="string", length=100) */
protected $contiene_libreta_limite_inferior;
/** @ORM\Column(type="string", length=100) */
protected $contiene_libreta_limite_superior;
/**
* @ORM\Column(type="string", length=100)
* @GRID\Column(title="Libretas omitidas")
*/
protected $omite_libreta;
/**
* @ORM\Column(type="string", length=100)
* @GRID\Column(title="Total libretas")
*/
protected $total_libretas;
/** @ORM\ManyToOne(targetEntity="PD\AppBundle\Entity\Juego")
* @ORM\JoinColumn(name="juego_id", referencedColumnName="id")
* */
protected $juego;
/** @ORM\ManyToOne(targetEntity="PD\AppBundle\Entity\Usuario") **/
protected $usuario;
/**
* @ORM\Column(type="datetime", nullable=true)
* @GRID\Column(title="Fecha creación")
*/
protected $fecha_creacion;
/**
* @var boolean
*
* @ORM\Column(name="estado", type="string", length=50)
* @GRID\Column(title="Estado")
*/
protected $estado;
/*
* 1 = CREADO
* 2 = ASIGNADO_A_SUPERVISOR
* 3 = FINALIZADO_CON_EXITO
* 4 = FINALIZADO_CON_OBSERVACIONES
* 5 = DESHABILITADO
*
*/
/**
* @ORM\OneToMany(targetEntity="PD\AppBundle\Entity\Libreta", mappedBy="caja", cascade={"remove", "persist"})
*/
protected $libretas;
public function __construct()
{
$this->fecha_creacion = new \DateTime();
$this->libretas = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set numero_carton
*
* @param string $numeroCarton
* @return Caja
*/
public function setNumeroCarton($numeroCarton)
{
$this->numero_carton = $numeroCarton;
return $this;
}
/**
* Get numero_carton
*
* @return string
*/
public function getNumeroCarton()
{
return $this->numero_carton;
}
/**
* Set contiene_libreta_limite_inferior
*
* @param string $contieneLibretaLimiteInferior
* @return Caja
*/
public function setContieneLibretaLimiteInferior($contieneLibretaLimiteInferior)
{
$this->contiene_libreta_limite_inferior = $contieneLibretaLimiteInferior;
return $this;
}
/**
* Get contiene_libreta_limite_inferior
*
* @return string
*/
public function getContieneLibretaLimiteInferior()
{
return $this->contiene_libreta_limite_inferior;
}
/**
* Set contiene_libreta_limite_superior
*
* @param string $contieneLibretaLimiteSuperior
* @return Caja
*/
public function setContieneLibretaLimiteSuperior($contieneLibretaLimiteSuperior)
{
$this->contiene_libreta_limite_superior = $contieneLibretaLimiteSuperior;
return $this;
}
/**
* Get contiene_libreta_limite_superior
*
* @return string
*/
public function getContieneLibretaLimiteSuperior()
{
return $this->contiene_libreta_limite_superior;
}
/**
* Set omite_libreta
*
* @param string $omiteLibreta
* @return Caja
*/
public function setOmiteLibreta($omiteLibreta)
{
$this->omite_libreta = $omiteLibreta;
return $this;
}
/**
* Get omite_libreta
*
* @return string
*/
public function getOmiteLibreta()
{
return $this->omite_libreta;
}
/**
* Set total_libretas
*
* @param string $totalLibretas
* @return Caja
*/
public function setTotalLibretas($totalLibretas)
{
$this->total_libretas = $totalLibretas;
return $this;
}
/**
* Get total_libretas
*
* @return string
*/
public function getTotalLibretas()
{
return $this->total_libretas;
}
/**
* Set juego
*
* @param \PD\AppBundle\Entity\Juego $juego
* @return Caja
*/
public function setJuego(\PD\AppBundle\Entity\Juego $juego)
{
$this->juego = $juego;
}
/**
* Get juego
*
* @return \PD\AppBundle\Entity\Juego
*/
public function getJuego()
{
return $this->juego;
}
public function __toString()
{
return $this->getNumeroCarton();
}
/**
* Set usuario
*
* @param \PD\AppBundle\Entity\Usuario $usuario
* @return Caja
*/
public function setUsuario(\PD\AppBundle\Entity\Usuario $usuario)
{
$this->usuario = $usuario;
return $this;
}
/**
* Get usuario
*
* @return \PD\AppBundle\Entity\Usuario
*/
public function getUsuario()
{
return $this->usuario;
}
/**
* Set fecha_creacion
*
* @param \DateTime $fechaCreacion
* @return Caja
*/
public function setFechaCreacion($fechaCreacion)
{
$this->fecha_creacion = $fechaCreacion;
return $this;
}
/**
* Get fecha_creacion
*
* @return \DateTime
*/
public function getFechaCreacion()
{
return $this->fecha_creacion;
}
/**
* Set estado
*
* @param string $estado
* @return Caja
*/
public function setEstado($estado)
{
$this->estado = $estado;
return $this;
}
/**
* Get estado
*
* @return string
*/
public function getEstado()
{
return $this->estado;
}
/**
* Add libretas
*
* @param \PD\AppBundle\Entity\Libreta $libretas
* @return Caja
*/
public function addLibreta(\PD\AppBundle\Entity\Libreta $libretas)
{
//$this->libretas[] = $libretas;
//return $this;
$libretas->setCaja($this);
$this->libretas->add($libretas);
return $this;
}
/**
* Remove libretas
*
* @param \PD\AppBundle\Entity\Libreta $libretas
*/
public function removeLibreta(\PD\AppBundle\Entity\Libreta $libretas)
{
$this->libretas->removeElement($libretas);
}
/**
* Get libretas
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getLibretas()
{
return $this->libretas;
}
}
<?php
namespace PD\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use APY\DataGridBundle\Grid\Mapping as GRID;
/**
* Libreta
*
* @ORM\Table()
* @ORM\Entity
*/
class Libreta
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="PD\AppBundle\Entity\Caja", inversedBy="libretas")
* @ORM\JoinColumn(name="caja_id", referencedColumnName="id", nullable=false)
* @Assert\Type(type="PD\AppBundle\Entity\Caja")
* @GRID\Column(field="caja.juego.nombre", title="Juego")
* @GRID\Column(field="caja.numero_carton", title="Caja")
*/
protected $caja;
/**
* @var string
*
* @ORM\Column(name="correlativo", type="string", length=10)
* @GRID\Column(title="Correlativo")
*/
private $correlativo;
/**
* @ORM\ManyToOne(targetEntity="PD\AppBundle\Entity\Usuario")
* @ORM\JoinColumn(name="vendedor_id", referencedColumnName="id", nullable=true)
* @Assert\Type(type="PD\AppBundle\Entity\Usuario")
* @GRID\Column(field="vendedor.nombre", title="Nombre vendedor")
* @GRID\Column(field="vendedor.apellidos", title="Apellidos vendedor")
*/
protected $vendedor;
/** @ORM\Column(name="precio_al_vendedor", type="decimal", scale=2) */
protected $precio_al_vendedor;
/** @ORM\Column(name="precio_acumulado", type="decimal", scale=2)
* @GRID\Column(title="Precio acumulado")
*/
protected $precio_acumulado;
/** @ORM\Column(name="premio_acumulado", type="decimal", scale=2)
* @GRID\Column(title="Premio acumulado")
*/
protected $premio_acumulado;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $fecha_asignacion_vendedor;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $fecha_estado_final;
/**
* @ORM\OneToMany(targetEntity="PD\AppBundle\Entity\Ticket", mappedBy="libreta", cascade={"persist"})
*/
protected $tickets;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set correlativo
*
* @param string $correlativo
* @return Libreta
*/
public function setCorrelativo($correlativo)
{
$this->correlativo = $correlativo;
return $this;
}
/**
* Get correlativo
*
* @return string
*/
public function getCorrelativo()
{
return $this->correlativo;
}
/**
* Set precio_al_vendedor
*
* @param string $precioAlVendedor
* @return Libreta
*/
public function setPrecioAlVendedor($precioAlVendedor)
{
$this->precio_al_vendedor = $precioAlVendedor;
return $this;
}
/**
* Get precio_al_vendedor
*
* @return string
*/
public function getPrecioAlVendedor()
{
return $this->precio_al_vendedor;
}
/**
* Set precio_acumulado
*
* @param string $precioAcumulado
* @return Libreta
*/
public function setPrecioAcumulado($precioAcumulado)
{
$this->precio_acumulado = $precioAcumulado;
return $this;
}
/**
* Get precio_acumulado
*
* @return string
*/
public function getPrecioAcumulado()
{
return $this->precio_acumulado;
}
/**
* Set fecha_asignacion_vendedor
*
* @param \DateTime $fechaAsignacionVendedor
* @return Libreta
*/
public function setFechaAsignacionVendedor($fechaAsignacionVendedor)
{
$this->fecha_asignacion_vendedor = $fechaAsignacionVendedor;
return $this;
}
/**
* Get fecha_asignacion_vendedor
*
* @return \DateTime
*/
public function getFechaAsignacionVendedor()
{
return $this->fecha_asignacion_vendedor;
}
/**
* Set fecha_estado_final
*
* @param \DateTime $fechaEstadoFinal
* @return Libreta
*/
public function setFechaEstadoFinal($fechaEstadoFinal)
{
$this->fecha_estado_final = $fechaEstadoFinal;
return $this;
}
/**
* Get fecha_estado_final
*
* @return \DateTime
*/
public function getFechaEstadoFinal()
{
return $this->fecha_estado_final;
}
/**
* Set vendedor
*
* @param \PD\AppBundle\Entity\Usuario $vendedor
* @return Libreta
*/
public function setVendedor(\PD\AppBundle\Entity\Usuario $vendedor = null)
{
$this->vendedor = $vendedor;
return $this;
}
/**
* Get vendedor
*
* @return \PD\AppBundle\Entity\Usuario
*/
public function getVendedor()
{
return $this->vendedor;
}
/**
* Set caja
*
* @param \PD\AppBundle\Entity\Caja $caja
* @return Libreta
*/
public function setCaja(\PD\AppBundle\Entity\Caja $caja = null)
{
$this->caja = $caja;
return $this;
}
/**
* Get caja
*
* @return \PD\AppBundle\Entity\Caja
*/
public function getCaja()
{
return $this->caja;
}
/**
* Constructor
*/
public function __construct()
{
$this->tickets = new \Doctrine\Common\Collections\ArrayCollection();
//$this->caja = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add tickets
*
* @param \PD\AppBundle\Entity\Ticket $tickets
* @return Libreta
*/
public function addTicket(\PD\AppBundle\Entity\Ticket $tickets)
{
//$this->tickets[] = $tickets;
$tickets->setLibreta($this);
$this->tickets->add($tickets);
return $this;
}
/**
* Remove tickets
*
* @param \PD\AppBundle\Entity\Ticket $tickets
*/
public function removeTicket(\PD\AppBundle\Entity\Ticket $tickets)
{
$this->tickets->removeElement($tickets);
}
/**
* Get tickets
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTickets()
{
return $this->tickets;
}
public function __toString()
{
return $this->correlativo;
}
/**
* Set premio_acumulado
*
* @param string $premioAcumulado
* @return Libreta
*/
public function setPremioAcumulado($premioAcumulado)
{
$this->premio_acumulado = $premioAcumulado;
return $this;
}
/**
* Get premio_acumulado
*
* @return string
*/
public function getPremioAcumulado()
{
return $this->premio_acumulado;
}
}
我不知道这里是否有在黑暗中拍摄的APILE。
但请记住,教义中的关系有自己的一面
如文件所述,考虑到这一原因
仅对关联的反面所做的更改将被忽略。
确保更新双向关联的两侧(或在
从学说的角度来看,至少是拥有方)
这有一些关于它的信息,我想它会帮助你。希望它至少能帮助你我在这里找到了一些信息
我需要使用$em->refresh($caja)在foreach语句之后。这解决了我的问题
foreach ($caja->getLibretas() as $libreta) {
$libreta->setCaja(NULL);
$em->remove($libreta);
}
$em->refresh($caja);
$caja->setEstado('DESHABILITADO');
$em->persist($caja);
$em->flush();
在每个循环步骤中刷新em内容的目的不是什么?这是db杀手。。。此外,您没有指定实体关系,这就是重点。我可以在循环中用flush删除实体,这就是为什么我要解决这个问题。当我访问我的电脑时,我会按照你的建议发布实体代码ok,caja有mappedBy(和OneToMany),所以反转的一面不是所有者。ok,caja有mappedBy(和OneToMany),所以反转的一面不是所有者。我不太明白的是“setCaja”,如果你试图删除它,为什么要把NULL放在那里?(Pareciera ser una baja logica lo que queres hacer),因此修改所有者(libreta)将其保留(因为SetCaja将不再指向他),原因是db caja中的所有者将不再指向libreta。考虑:如果您计划在该上下文中继续使用caja对象,那么您必须从对象本身删除libreta。对不起,我的英语。我想删除每一个caja的文库一个特定的caja。因为某种原因,我没有在循环中使用flush(),所以libreta不会从caja中删除。在其他代码中,我在循环外使用了flush(),一切正常。这就是我想解决的问题。好吧,我现在真的不知道如何帮助你。。。。尝试两件事。1-更改刷新顺序,2-同时从caja中删除libreta。我不相信这会奏效,但这是一个5分钟的尝试。对不起,我帮不了你更多。可能是房地产出了问题,例如,如果vendedor有cajas,cajas有libretas,vendedor是所有者,并且正在级联,那么您的libretas可能会被删除并重新生成。祝你好运,我的朋友(这是两个不同的意图)