codeigniter - 在控制器中使用模型结果来处理另一个模型查询

时间:2013-10-10 08:48:12

标签: php database codeigniter model

我有点卡住了。

我有以下控制器功能。这100%没有问题。

function new_blank_order_lines() 
{
          $data = array(
           'project' =>$this->input->post('project'),
           'millcoords' => $this->sales_model->get_mill_coords($this->input->post('mill')),
           'custcoords' => $this->sales_model->get_cust_coords($this->input->post('deliveryaddressid')),

          );
    $this->load->view('sales/new_blank_order_lines',$data);

我想要做的是将更多模型信息传递给视图。然而,我的问题是我需要先前模型查询的结果用于我的新模型查询。所以我想按如下方式添加到数组中:

   $data = array(
       'project' =>$this->input->post('project'),
       'millcoords' => $this->sales_model->get_mill_coords($this->input->post('mill')),
       'custcoords' => $this->sales_model->get_cust_coords($this->input->post('deliveryaddressid')),
       'customersinrange' =>  $this->sales_model->get_customers_in_range($millcoords,$custcoords),            
      );

所以我想将两个值传递给模型函数get_customers_in_range。我想传递millcoords的结果和custcoords的结果。

如何实现这一点,以便控制器可以使用模型查询的结果来执行新的模型查询?

一如既往地谢谢 莱恩

3 个答案:

答案 0 :(得分:4)

试试这个:

function new_blank_order_lines() 
{
    $data                       = array();
    $data['project']            = $this->input->post('project', true);
    $data['millcoords']         = $this->sales_model->get_mill_coords( $this->input->post('mill') );
    $data['custcoords']         = $this->sales_model->get_cust_coords($this->input->post('deliveryaddressid'));
    $data['customersinrange']   = $this->sales_model->get_customers_in_range($data['millcoords'], $data['custcoords']);
    $this->load->view('sales/new_blank_order_lines',$data);

答案 1 :(得分:2)

$millcoords = $this->sales_model->get_mill_coords($this->input->post('mill');
$custcoords = $this->sales_model->get_cust_coords($this->input->post('deliveryaddressid');
$customersinrange = $this->sales_model->get_customers_in_range($millcoords,$custcoords);
    $data = array(
           'project' =>$this->input->post('project'),
           'millcoords' => $millcoords,
           'custcoords' => $custcoords,
           'customersinrange' => $customersinrange,            
          );


    $this->load->view('sales/new_blank_order_lines',$data);

答案 2 :(得分:2)

试试这个:

    $millcoords = $this->sales_model->get_mill_coords($this->input->post('mill'));
$custcoords = $this->sales_model->get_cust_coords($this->input->post('deliveryaddressid'));
    $data = array(
           'project' =>$this->input->post('project'),
           'millcoords' => $millcoords,
           'custcoords' => $custcoords,
           'customersinrange' =>  $this->sales_model->get_customers_in_range($millcoords,$custcoords),            
          );