Question

Well, I have 3 tables: sorty with fields id, name, fStart, fEnd bol with fields id, fol, and bol_sorty with id, bol_id, sorty_id

When I add a new register to sorty, it must add to bol the number of items from fstart to fEnd. I know how to create a new object and insert it to the table but I can't undesteand anything about relations [I'm trying], Already read the code igniter and datamapper guide, I'm new on this.

something like this: MODEL:

 public function agregar() {
        $nombre=$this -> input -> post('nombre');
        $folio_inicial=$this -> input -> post('folioInicial');
        $folio_final= $this -> input -> post('folioFinal');

        $u = new Sorteo();
        $u -> nombre = $nombre;
        $u -> folio_inicial = $folio_inicial;
        $u -> folio_final = $folio_final;
        $u -> costo = $this -> input -> post('costo');
        $u -> save();

        /*for( $i=$folio_inicial; $i<=$folio_final; $i++ ){

            $b=new Boleto();
            $b->folio=$i;
            $b->estado=2;
            $b->condicion=2;
            $b->campus=4;
            $b->save();
            //$u->save($b);  
        }

       /* $this->load->model('sorteos/model_boleto');
        $this->model_boleto->agregar($nombre, $folio_final,$folio_inicial);

        /*if ($u -> save())
            return true;
        else
            return false;*/
    }

AND CONTROLLER

 public function procesar_sorteo_nuevo() {
        $this -> form_validation -> set_rules('nombre', 'Nombre', 'trim|required|is_unique[sorteos.nombre]');
        $this -> form_validation -> set_rules('folioInicial', 'Folio Inicial', 'trim|required|callback_chequear');
        $this -> form_validation -> set_rules('folioFinal', 'Folio Final', 'trim|required');
        $this -> form_validation -> set_rules('costo', 'Costo', 'trim|required');

        if ($this -> form_validation -> run()) {
            if ($this -> model_sorteo -> agregar()) {
                echo "Sorteo Creado";
            } else {
                echo "ERROR FATAL";
            }
            //redirect('/sorteos/sorteos/');
        }

    }
Was it helpful?

Solution

For each table add a parent id to match the child table.

    sorty---------id-------fstart------fend
    bol-----------id-------sortyid  Parent For Sorty
    bol_sorty-----id-------bolid-------sortyid   parent for sorty and bol.
    and if bol_sorty needs a parent in both.
    bol_sorty-----id-------parent_bol----parentsorty--
    Then--select from bol_sorty where bolid="'.$value.'" and sortyid="'.$value.'", etc.

Sample of multi Insert Query.
if(mysql_query('insert into sorty (id, fstart, fend) select "'.$id1.'", "'.$fstart.'", "'.$fend.'" from sorty where id="'.$id.'"') and mysql_query('insert into bol (id, bol, bolsorty) select "'.$bolid.'"--etc-- where id="'.$id.'" and bolid="'.$bolid.'" '))---etc.can add a third insert using---- and (mysql_query( insert into bol_sorty(etc.) select "'.$id.'",--etc. I would suggest naming field id's for each subsequent table---id, bid , sid and set the parents---pid1, pid2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top