【问题标题】:Datamapper php relations [closed]Datamapper php关系[关闭]
【发布时间】:2013-01-22 06:23:52
【问题描述】:

嗯,我有 3 张桌子: 带有字段 id、name、fStart、fEnd 的 sorty 具有字段 id、fol 和 bol_sorty with id, bol_id, sorty_id

当我向 sorty 添加一个新寄存器时,它必须添加从 fstart 到 fEnd 的项目数。 我知道如何创建一个新对象并将其插入表中,但我无法取消任何关于关系的内容 [我正在尝试],已经阅读了代码点火器和数据映射器指南,我是新手。

类似这样的: 型号:

 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;*/
    }

和控制器

 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/');
        }

    }

【问题讨论】:

  • 可能建议阅读一些企业应用程序架构模式 - 很棒的书,也有一些在线内容。具体阅读Data Source Architectural Patterns。假设具有 RDBMS 和实体关系的基本知识。
  • 你能解释一下吗?当您插入一个表时,您还想更新另一个表吗?
  • 是的,插入 SORTY:fstart=1 和 fend=3,然后插入 BOL fol =1、fol=2 和 fol=3,然后插入到 BOL_SORTY 的关系。但是我的尝试失败了。我解释得好吗?
  • 非常感谢,我会做的
  • 如果你用你正在编写的真实 SQL 查询来解释你的问题,或者用你的代码更好地解释你的问题,那会更好。这样我才能完全理解你的问题。

标签: php codeigniter relationship datamapper codeigniter-datamapper


【解决方案1】:

为每个表添加一个父 id 以匹配子表。

    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.

多插入查询示例。
if(mysql_query('insert into sorty (id, fstart, fend) select "'.$id1.'", "'.$fstart.'", "'.$fend.'" from sorty where id="'. $id.'"') 和 mysql_query('insert into bol (id, bol, bolsorty) select "'.$bolid.'"--etc-- where id="'.$id.'" and bolid=" '.$bolid.'" '))---etc.can 添加第三个插入使用---- 和 (mysql_query( insert into bol_sorty(etc.) select "'.$id.'",--etc.我建议为每个后续表命名字段 id ---id、bid、sid 并设置父母---pid1、pid2。

【讨论】:

  • 谢谢,这就是我的方式,但我应该做不同的,因为代码向上。
猜你喜欢
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多