Created
December 12, 2018 03:48
-
-
Save RodolVelasco/f35fa211f5324773e18554d7441217be to your computer and use it in GitHub Desktop.
Symfony 2.8 must code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Symfony 2.8 must code |
Cloning in PHP
Cloning objects with deep level 3 or more
public function __clone() {
if ($this->id) {
$this->package = clone $this->package;
$imagenes = $this->getImagenes();
$this->imagenes = new ArrayCollection();
if(count($imagenes) > 0){
foreach ($imagenes as $imagen) {
$cloneImagen = clone $imagen;
$this->imagenes->add($cloneImagen);
$cloneImagen->setProduct($this);
}
}
}
}
Ejemplo en sistema AEPP instalado en juegos_esa.
private function duplicateProcess($entity)
{
$dupEntity = clone $entity;
// dump([
// $entity,
// $dupEntity]
// );exit;die;
$ar = [];
array_push($ar, $dupEntity->getNombre() . ' => ' . $dupEntity->getId());
foreach($dupEntity->getCriterios() as $cri) {
array_push($ar, $cri->getEtiqueta() . ' ' . $cri->getNombre() . ' => ' . $cri->getId());
foreach($cri->getPreguntas() as $preg) {
array_push($ar, $preg->getNombre(). ' => ' . $preg->getId());
foreach($preg->getGuias() as $guia) {
array_push($ar, $guia->getNombre() . ' => ' . $guia->getId());
}
}
}
dump($ar);exit;die;
return true;
}
Luego en Autoevaluación.php
public function __clone()
{
if ($this->id) {
$this->setId(null);
$this->setEstado(false);
$criterios = $this->getCriterios();
$this->criterios = new ArrayCollection();
if(count($criterios) > 0){
foreach ($criterios as $criterio) {
$cloneCriterio = clone $criterio;
$this->criterios->add($cloneCriterio);
$cloneCriterio->setAutoEvaluacion($this);
}
}
}
}
Luego en Criterio.php
public function __clone()
{
if ($this->id) {
$this->setId(null);
//$this->children = clone $this->children;
// $this->preguntas = clone $this->preguntas;
$preguntas = $this->getPreguntas();
$this->preguntas = new ArrayCollection();
if(count($preguntas) > 0){
foreach ($preguntas as $pregunta) {
$clonePregunta = clone $pregunta;
$this->preguntas->add($clonePregunta);
$clonePregunta->setCriterio($this);
}
}
}
}
Luego en Pregunta.php
public function __clone()
{
if ($this->id) {
$this->setId(null);
//$this->guias = clone $this->guias;
$guias = $this->getGuias();
$this->guias = new ArrayCollection();
if(count($guias) > 0){
foreach ($guias as $guia) {
$cloneGuia = clone $guia;
$this->guias->add($cloneGuia);
$cloneGuia->setPregunta($this);
}
}
}
}
Luego en Guia.php
public function __clone()
{
if ($this->id) {
$this->setId(null);
}
}
Controller
Acceso denegado
throw $this->createAccessDeniedException("No tiene permisos para acceder a esta página!");
No se encuentra el producto
throw $this->createNotFoundException('The product does not exist');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Composer
composer require symfony/intl
composer require symfony/polyfill-iconv
composer install --ignore-platform-reqs
php bin/console doctrine:schema:update --force