【问题标题】:Codeigniter Grocery Crud extend set_relation() function?Codeigniter Grocery Crud 扩展 set_relation() 函数?
【发布时间】:2011-11-15 00:19:57
【问题描述】:

有人知道如何扩展set_relation() 吗?

这是一个想法,基于 Grocery Crud 员工和办公室表的示例

$crud->set_relation('officeCode','offices','city');

代码输出将在下拉列表中显示所有城市,但我想扩展 set_relation() 以按州过滤所有城市以显示在下拉列表中?

例如,我想在下拉列表中显示亚利桑那州的所有城市,有人在使用杂货杂货之前这样做了吗?

参考示例:

Grocery Crud

【问题讨论】:

    标签: codeigniter crud grocery-crud


    【解决方案1】:

    我相信你要找的就在这里 --> http://www.grocerycrud.com/crud/function_name/set_model

    这将允许您以您想要的方式扩展杂货店 CRUD 的功能。 我实际上并没有冒险创建自己的自定义功能,但这是您想要的方式。

    【讨论】:

      【解决方案2】:

      我认为你需要这个:

      function employees_management()
      {
          $crud = new grocery_CRUD();
      
          $crud->set_theme('datatables');
          $crud->set_table('employees');
          // add this line
          $crud->callback_edit_field('officeCode',array($this,'_call_back_offices'));
          $crud->callback_add_field('officeCode',array($this,'_call_back_offices'));
      
          $crud->display_as('officeCode','Office City');
          $crud->set_subject('Employee');
      
          $crud->set_relation('officeCode','offices','city');
      
          $output = $crud->render();
      
          $this->_example_output($output);
      }
      
      function _call_back_offices()
      {
          $this->load->model('user_model'); // your model
          $data = $this->user_model->getYou('offices','officeCode, city', "state = '".$this->session->userdata('state')."'"); // iam using session here
          $hasil ='<select name="officeCode">';
          foreach($data as $x)
          {
              $hasil .='<option value="'.$x->officeCode.'">'.$x->city.'</option>';
          }
          return $hasil.'</select>';
      }
      
      example user_model code:
      
      function getYou($tabel,$field = '*', $cond ='')
      {
          $this->db->select($field);
          $this->db->from($tabel);
          if(!empty($cond)) $this->db->where($cond);
          return $this->db->get()->result();
      }
      

      【讨论】:

        【解决方案3】:
        $crud->set_relation('officeCode','offices','city',array('country' => 'Arizona'));
        

        这适用于 v.1.2 或更高版本

        【讨论】:

          【解决方案4】:

          我知道我的回答有些过时,现在可能对 OP 没有用处,但我猜有很多人可能会在这篇文章中寻找灵感来解决他们自己的 set_relation 问题。我对此提出了一个更简单的解决方案......(公平地说,我不知道 set_relation() 的第 4 个参数何时可用。)

          ...
          $state = "AZ"; // or however you get the state you want to filter by
          $crud->set_relation('officeCode','offices','city', 'officeCode IN (SELECT officeCode FROM offices WHERE state='.$state.')');
          ...
          

          瞧!真正的grocery_CRUD很棒!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-04-19
            • 2015-11-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多