I am trying to remove Libreta-related objects from Caja objects, but the removal does not work when I use flush () outside of the foreach code, when in other code or examples I can do the same with a flash outside the code. Why?
This is part of the code that works.
Working example
$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'));
}
But when I try to use the same code, but with this modification, I cannot delete related objects, and I do not get any errors
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;
class Caja
{
protected $id;
protected $numero_carton;
protected $contiene_libreta_limite_inferior;
protected $contiene_libreta_limite_superior;
protected $omite_libreta;
protected $total_libretas;
protected $juego;
protected $usuario;
protected $fecha_creacion;
protected $estado;
protected $libretas;
public function __construct()
{
$this->fecha_creacion = new \DateTime();
$this->libretas = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setNumeroCarton($numeroCarton)
{
$this->numero_carton = $numeroCarton;
return $this;
}
public function getNumeroCarton()
{
return $this->numero_carton;
}
public function setContieneLibretaLimiteInferior($contieneLibretaLimiteInferior)
{
$this->contiene_libreta_limite_inferior = $contieneLibretaLimiteInferior;
return $this;
}
public function getContieneLibretaLimiteInferior()
{
return $this->contiene_libreta_limite_inferior;
}
public function setContieneLibretaLimiteSuperior($contieneLibretaLimiteSuperior)
{
$this->contiene_libreta_limite_superior = $contieneLibretaLimiteSuperior;
return $this;
}
public function getContieneLibretaLimiteSuperior()
{
return $this->contiene_libreta_limite_superior;
}
public function setOmiteLibreta($omiteLibreta)
{
$this->omite_libreta = $omiteLibreta;
return $this;
}
public function getOmiteLibreta()
{
return $this->omite_libreta;
}
public function setTotalLibretas($totalLibretas)
{
$this->total_libretas = $totalLibretas;
return $this;
}
public function getTotalLibretas()
{
return $this->total_libretas;
}
public function setJuego(\PD\AppBundle\Entity\Juego $juego)
{
$this->juego = $juego;
}
public function getJuego()
{
return $this->juego;
}
public function __toString()
{
return $this->getNumeroCarton();
}
public function setUsuario(\PD\AppBundle\Entity\Usuario $usuario)
{
$this->usuario = $usuario;
return $this;
}
public function getUsuario()
{
return $this->usuario;
}
public function setFechaCreacion($fechaCreacion)
{
$this->fecha_creacion = $fechaCreacion;
return $this;
}
public function getFechaCreacion()
{
return $this->fecha_creacion;
}
public function setEstado($estado)
{
$this->estado = $estado;
return $this;
}
public function getEstado()
{
return $this->estado;
}
public function addLibreta(\PD\AppBundle\Entity\Libreta $libretas)
{
$libretas->setCaja($this);
$this->libretas->add($libretas);
return $this;
}
public function removeLibreta(\PD\AppBundle\Entity\Libreta $libretas)
{
$this->libretas->removeElement($libretas);
}
public function getLibretas()
{
return $this->libretas;
}
}
Libreta.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;
class Libreta
{
private $id;
protected $caja;
private $correlativo;
protected $vendedor;
protected $precio_al_vendedor;
protected $precio_acumulado;
protected $premio_acumulado;
protected $fecha_asignacion_vendedor;
protected $fecha_estado_final;
protected $tickets;
public function getId()
{
return $this->id;
}
public function setCorrelativo($correlativo)
{
$this->correlativo = $correlativo;
return $this;
}
public function getCorrelativo()
{
return $this->correlativo;
}
public function setPrecioAlVendedor($precioAlVendedor)
{
$this->precio_al_vendedor = $precioAlVendedor;
return $this;
}
public function getPrecioAlVendedor()
{
return $this->precio_al_vendedor;
}
public function setPrecioAcumulado($precioAcumulado)
{
$this->precio_acumulado = $precioAcumulado;
return $this;
}
public function getPrecioAcumulado()
{
return $this->precio_acumulado;
}
public function setFechaAsignacionVendedor($fechaAsignacionVendedor)
{
$this->fecha_asignacion_vendedor = $fechaAsignacionVendedor;
return $this;
}
public function getFechaAsignacionVendedor()
{
return $this->fecha_asignacion_vendedor;
}
public function setFechaEstadoFinal($fechaEstadoFinal)
{
$this->fecha_estado_final = $fechaEstadoFinal;
return $this;
}
public function getFechaEstadoFinal()
{
return $this->fecha_estado_final;
}
public function setVendedor(\PD\AppBundle\Entity\Usuario $vendedor = null)
{
$this->vendedor = $vendedor;
return $this;
}
public function getVendedor()
{
return $this->vendedor;
}
public function setCaja(\PD\AppBundle\Entity\Caja $caja = null)
{
$this->caja = $caja;
return $this;
}
public function getCaja()
{
return $this->caja;
}
public function __construct()
{
$this->tickets = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addTicket(\PD\AppBundle\Entity\Ticket $tickets)
{
$tickets->setLibreta($this);
$this->tickets->add($tickets);
return $this;
}
public function removeTicket(\PD\AppBundle\Entity\Ticket $tickets)
{
$this->tickets->removeElement($tickets);
}
public function getTickets()
{
return $this->tickets;
}
public function __toString()
{
return $this->correlativo;
}
public function setPremioAcumulado($premioAcumulado)
{
$this->premio_acumulado = $premioAcumulado;
return $this;
}
public function getPremioAcumulado()
{
return $this->premio_acumulado;
}
}
Any ideas how to solve this to make it more efficient.
source
share