【问题标题】:GroceryCrud Table Relationships set_model (join tables) [EDITED]GroceryCrud 表关系 set_model(连接表)[已编辑]
【发布时间】:2012-04-23 14:32:52
【问题描述】:

我有两张这样的表:

CREATE TABLE `tblFacilityHrs` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `uid` varchar(45) DEFAULT NULL,
          `title` varchar(100) DEFAULT NULL,
          `description` text,
          PRIMARY KEY (`id`),
          KEY `key_uid` (`uid`)
        ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1$$

        CREATE TABLE `tblFacilityHrsDateTimes` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `owner_uid` varchar(45) DEFAULT NULL,
          `startDate` date DEFAULT NULL,
          `endDate` date DEFAULT NULL,
          `startTime` time DEFAULT NULL,
          `endTime` time DEFAULT NULL,
          `days` int(2),
          `recurrence` int(1) DEFAULT NULL,
          PRIMARY KEY (`id`),
          KEY `fk_fh_owneruid` (`owner_uid`),
          CONSTRAINT `fk_fh_owneruid` FOREIGN KEY (`owner_uid`) REFERENCES `tblFacilityHrs` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=latin1$$

id uid 标题描述位置
8ada0ceabd40d509c3fb38f2822a97de11bc6628 游泳课家长和孩子 CRC 2 543a6ed0005ff6a0a7fc99cc2f9715d86804ecb0 游泳课 1 级,第 1 节 3 7d219b64be6dc706135bdad3e7c2f0d56cb7f353 游泳 课程级别 2 4 f7c91e2f1daa9c696c22f5aa5736c167d1ba9f94 游泳 课程 3 级 5 262f06fb75645248162aa983f610ec7959a2011b 游泳 课程级别 4 6 51f9f552ffc5fa4bc8b4e7f914fb22b3b0920c2e 自行车 275 参加此计划并参加 4 节健身自行车课程并获得 一个免费的水瓶!参与者只能注册1次。这是一个 免费程序!在多功能厅注册。 7 0cca3515ec8ee990c863e474fee634ae94d382c2 健身护照 北欧健身课程,盖上护照并赢取免费 T恤!在 4 月 10 日至 5 月 1 日期间参加 8 节健身课程并获得 免费 T 恤。

id owner_uid startDate endDate startTime endTime 天数重复
8ada0ceabd40d509c3fb38f2822a97de11bc6628 2012 年 4 月 13 日 2012 年 4 月 13 日 0:00:00 NULL NULL 无 2 543a6ed0005ff6a0a7fc99cc2f9715d86804ecb0 NULL NULL 12:30:00 2:00:00 2 3 7d219b64be6dc706135bdad3e7c2f0d56cb7f353 空 NULL NULL NULL NULL NULL 4 f7c91e2f1daa9c696c22f5aa5736c167d1ba9f94 NULL NULL NULL NULL NULL NULL 5 262f06fb75645248162aa983f610ec7959a2011b NULL NULL NULL NULL NULL NULL 6 51f9f552ffc5fa4bc8b4e7f914fb22b3b0920c2e NULL NULL NULL NULL NULL 空 7 0cca3515ec8ee990c863e474fee634ae94d382c2 空 空 空 空 NULL NULL

在我的控制器文件夹中,我有一个名为 main.php 的文件,其中包含以下代码:

...
function fitnessSchedule()
    {
        $this->config->set_item('url_suffix', '');
        $crud = new grocery_CRUD();

        $crud->set_model('schedule_model');
        $crud->set_table('tblFitnessClasses');
        $crud->join_table('tblFitnessClasses','tblFitnessClassDateTimes');
        $crud->columns('title','description','location','startDate','endDate','startTime', 'endTime', 'days', 'recurrance');
        $crud->display_as('title','Event')
             ->display_as('description','Description')
             ->display_as('location','Location')
             ->display_as('startDate','Start Date')
             ->display_as('endDate','End Date')
             ->display_as('startTime','Start Time')
             ->display_as('endTime','End Time');
        $crud->required_fields('title','location');
        $crud->set_subject('Event');              

        $output = $crud->render();
        $this->_example_output($output);    
    }

    function _example_output($output = null)    
    {
        $this->load->view('main_view', $output);    
    }
...

在我的模型文件夹中,我有这个:

<?php
class schedule_model extends grocery_CRUD_Model
{
        function join_table($table1, $table2)
        {
         if($this->$table1 === null)
          return false;

         $select = "{$this->$table1}.*";
         $select .=",$table2.startDate, $table2.endDate, $table2.startTime, $table2.endTime, $table2.days, $table2.recurrence";

         if(!empty($this->relation))
          foreach($this->relation as $relation)
          {
           list($field_name , $related_table , $related_field_title) = $relation;
           $unique_join_name = $this->_unique_join_name($field_name);
           $unique_field_name = $this->_unique_field_name($field_name);

        if(strstr($related_field_title,'{'))
                $select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
           else   
                $select .= ", $unique_join_name.$related_field_title as $unique_field_name";

           if($this->field_exists($related_field_title))
                $select .= ", {$this->$table1}.$related_field_title as '{$this->$table1}.$related_field_title'";
          }

         $this->db->select($select, false);


         $this->db->join('uid', '$table2.owner_uid = $table1.uid');

         $results = $this->db->get($this->$table1)->result();

         return $results;
        }

        /* function join_table($table1, $table2)
        {
                $this->db->select('$table1.*');
                $this->db->join('$table2','$table1.uid = $table2.owner_uid','left');
                $this->db->get('$table1');
         }*/
}
?>

我收到此错误:

致命错误:调用未定义的方法grocery_CRUD::join_table() C:\xampp\htdocs\codeigniter\application\controllers\main.php 上线 234

我基本上试图通过 UID(uid 到 owner_uid)将 tblFacilityHrs 与 tblFacilityHrsDateTimes 连接起来。我想同时显示两个表格,这样当用户编辑表格时,他们不仅可以编辑事件的名称/位置,还可以编辑其时间/日期等。

参考:http://www.grocerycrud.com/documentation/options_functions/set_model

【问题讨论】:

    标签: php codeigniter-2 grocery-crud


    【解决方案1】:

    我有同样的问题,以下是我所做的解决方案。

    1. 使用 mySQL 连接语句为我要连接的表创建一个视图
    2. 查找状态 ($cur_state=$crud->getState();) 并查看它是否为“list、ajaxlist、读取或成功”,然后我设置了 $crud->set_table('VIEW_NAME') ; 并设置主键 $crud->set_primary_key('KEY','VIEW_NAME');
    3. 对于 else 部分,我使用表名本身,以便它有助​​于添加、编辑操作。

    这个解决方案效果很好。

    $crud = new grocery_CRUD();
    $cur_state=$crud->getState();
    $crud->set_subject('Patient Notes');        
    
    /* Use the mySQL view to display the data with related tables */
    if(($cur_state=="list") || ($cur_state=="ajaxlist") || ($cur_state=="read") ||     ($cur_state=="success"))     
    {
       $crud->set_table('patientsnotes_vw');
       $crud->columns('noteID','Clinic_No','note','noteCreated','username');
       $crud->set_primary_key('noteID','patientsnotes_vw');             
    
       if($cur_state=="read")
       {
        $crud->unset_fields('noteCreatedBy','user_id','patientID');                      
           }
    }
    else
    {
            /* Use the patient_note table itself for add/edit operation */
            $crud->set_table('patient_notes');
        $crud->columns('noteID','Clinic_No','note','noteCreated','username');
        $crud->required_fields('note');
        $crud->field_type('noteCreatedBy', 'hidden', $this->userID);
        $crud->field_type('patientID', 'hidden', 1);
        $crud->unset_add_fields('noteCreated');
        $crud->unset_edit_fields('noteCreated');        
     }
    
      $crud->display_as('username','Note Created by');
      $crud->display_as('noteCreated','Note Created at');       
      $output = $crud->render();
    

    【讨论】:

      【解决方案2】:

      this forum thread 中,web-johnny 声明此功能尚无法实现。

      这是杂货店 CRUD 没有的 1-1 关系 功能还没有。这取决于我将有多少时间 将来这样做和/或在此之前我是否有任何好的捐赠。

      这个功能很重要,做起来也很复杂, 但我认为它将为许多用户节省大量时间 起来。

      也许如果您提供 web-johnny 认为是好的捐赠,他可能会实施。

      【讨论】:

      • 我已将我的问题编辑得更具体。这可以通过 set_model 完成。
      【解决方案3】:

      grocery_CRUD 类 ($crud = new foods_CRUD(); ) 本地化在文件夹库中,您在模型中实现 join_table。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-02
        • 2014-03-26
        • 1970-01-01
        相关资源
        最近更新 更多