public function executeDuplicate()
 {
     $tabla = $this->getTablaOrCreate();
     //DUPLICAR LA TABLA
     $mitabla = new Tabla();
     $tabla->copyInto($mitabla);
     $mitabla->setNombre("Copia de " . $tabla->getNombre());
     $mitabla->save();
     //DUPLICAMOS CAMPOS
     $campos = $tabla->getRelCampoTablasJoinCampo();
     foreach ($campos as $camp) {
         $campo = $camp->getCampo();
         $items = $campo->getItemBases();
         $micampo = new Campo();
         $campo->copyInto($micampo);
         $micampo->save();
         //DUPLICAMOS ITEMS
         foreach ($items as $item) {
             $miitem = new ItemBase();
             $item->copyInto($miitem);
             $miitem->setIdCampo($micampo->getIdCampo());
             $miitem->save();
         }
         $micamp = new RelCampoTabla();
         $micamp->setIdCampo($micampo->getIdCampo());
         $micamp->setIdTabla($mitabla->getIdTabla());
         $micamp->save();
     }
     return $this->redirect('tablas/list');
 }
 protected function saveCampo($campo)
 {
     if (!$campo->getIdCampo()) {
         if (!$campo->esTipoLista()) {
             //un campo "no lista" debe tener un item asociado.
             $item_aux = new ItemBase();
             $item_aux->setOrden(1);
             $campo->addItemBase($item_aux);
         }
     }
     // Update many-to-many for "campoTablas"
     $campo->initRelCampoTablas();
     $c = new Criteria();
     $c->add(RelCampoTablaPeer::ID_CAMPO, $campo->getPrimaryKey());
     RelCampoTablaPeer::doDelete($c);
     if (!$campo->getEsGeneral()) {
         if ($this->getRequestParameter('id_tabla')) {
             $rel = new RelCampoTabla();
             $rel->setIdTabla($this->getRequestParameter('id_tabla'));
             $campo->addRelCampoTabla($rel);
         } else {
             $ids = $this->getRequestParameter('associated_campo_tablas');
             if (is_array($ids)) {
                 foreach ($ids as $id) {
                     $rel = new RelCampoTabla();
                     $rel->setIdTabla($id);
                     $campo->addRelCampoTabla($rel);
                 }
             }
         }
     }
     $campo->save();
 }
 public function executeDuplicate()
 {
     $empresa = $this->getEmpresaOrCreate();
     //DUPLICAR LA EMPRESA
     $miempresa = new Empresa();
     $empresa->copyInto($miempresa);
     $miempresa->setNombre("Copia de " . $empresa->getNombre());
     $miempresa->save();
     $tablas_equiv = array();
     $campos_cambiar = array();
     //DUPLICAMOS TABLAS
     $tablas = $empresa->getTablas();
     foreach ($tablas as $tabla) {
         $mitabla = new Tabla();
         $tabla->copyInto($mitabla);
         $mitabla->setIdEmpresa($miempresa->getIdEmpresa());
         $mitabla->save();
         //GUARDAMOS LAS EQUIVALENCIAS DE LAS TABLAS PARA LUEGO CAMBIAR LAS RELACIONES
         $tablas_equiv[$tabla->getIdtabla()] = $mitabla->getIdTabla();
         $campos = $tabla->getRelCampoTablasJoinCampo();
         foreach ($campos as $camp) {
             $campo = $camp->getCampo();
             $items = $campo->getItemBases();
             $micampo = new Campo();
             $campo->copyInto($micampo);
             $micampo->setIdEmpresa($miempresa->getIdEmpresa());
             $micampo->save();
             if ($micampo->getTipo() == CampoPeer::ID_TABLA) {
                 $campos_cambiar[] = $micampo->getIdCampo();
             }
             //DUPLICAMOS ITEMS
             foreach ($items as $item) {
                 $miitem = new ItemBase();
                 $item->copyInto($miitem);
                 $miitem->setIdCampo($micampo->getIdCampo());
                 $miitem->save();
             }
             $micamp = new RelCampoTabla();
             $micamp->setIdCampo($micampo->getIdCampo());
             $micamp->setIdTabla($mitabla->getIdTabla());
             $micamp->save();
         }
     }
     //AHORA CAMBIAMOS LOS ENLACES A LAS TABLAS
     foreach ($campos_cambiar as $camp) {
         $campo = CampoPeer::retrieveByPk($camp);
         $campo->setValorTabla($tablas_equiv[$campo->getValorTabla()]);
         $campo->save();
     }
     return $this->redirect('tablas/list');
 }
 /**
  * Adds an object to the instance pool.
  *
  * Propel keeps cached copies of objects in an instance pool when they are retrieved
  * from the database.  In some cases -- especially when you override doSelect*()
  * methods in your stub classes -- you may need to explicitly add objects
  * to the cache in order to ensure that the same objects are always returned by doSelect*()
  * and retrieveByPK*() calls.
  *
  * @param      RelCampoTabla $value A RelCampoTabla object.
  * @param      string $key (optional) key to use for instance map (for performance boost if key was already calculated externally).
  */
 public static function addInstanceToPool(RelCampoTabla $obj, $key = null)
 {
     if (Propel::isInstancePoolingEnabled()) {
         if ($key === null) {
             $key = serialize(array((string) $obj->getIdCampo(), (string) $obj->getIdTabla()));
         }
         // if key === null
         self::$instances[$key] = $obj;
     }
 }