codeigniter - 在控制器中使用模型结果来调用模型中的不同函数

时间:2015-06-24 08:55:36

标签: codeigniter model-view-controller

我希望根据用户输入从数据库中检索结果后,将结果传递给模型中的控制器。返回到控制器的值将与通过同一表单提交的其他变量一起插入另一个表中。当我尝试运行我的代码时,它给出了下面的错误。我使用的是codeigniter 3.0.0

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/Service_providers.php
Line Number: 68

也是这个

Error Number: 1048
Column 'bs_name' cannot be null
INSERT INTO `merchants` (`bs_name`, `business_no`, `phone_no`, `location`, `licence_no`, `commission`, `balance`, `email`, `username`, `password`) VALUES (NULL, '4', '4', NULL, '4', '4', '4', 'muokid3@gmail.com', '4', 'e4da3b7fbbce2345d7772b0674a318d5')
Filename: C:\xampp\htdocs\mafya\system\database\DB_driver.php

Line Number: 330

这是我的代码:

控制器

function add()

    {
        $facility_code = $this->input->post('facilityno');

        $data['fa_details'] = $this->Service_provider_model->getFacilityDetails($facility_code);

        if ($fa_details = null) 
        {
            echo "The Service Provider is not Recognized by the Government";
        }
        else
        {
            foreach ($data['fa_details'] as $detail) 
            {
                $bs_name = $detail->facility_name;
                $location = $detail->district;
            }

            $s_provider = array('bs_name' => $bs_name,
                    'business_no' => $this->input->post('businessno'),
                    'phone_no' => $this->input->post('phoneno'),
                    'location' => $location,
                    'licence_no' => $this->input->post('licenseno'),
                    'commission' => $this->input->post('commission'),
                    'balance' => $this->input->post('balance'),
                    'email' => $this->input->post('email'),
                    'username' => $this->input->post('username'),
                    'password' => md5($this->input->post('password')));

            if ($this->Service_provider_model->save($s_provider)) 
            {
                echo "Service Provider Successfully Added";
            }
            else
            {
                echo "Service Provider NOT Added";
            }
        }


    }

模型:

function getFacilityDetails($facility_code)
    {
        $facility_details = 'facility_details';
        $this->db->where('facility_code', $facility_code);
        return $this->db->get($this->$facility_details);
    }

function save($s_provider){
        $this->db->insert($this->merchants, $s_provider);
        return $this->db->insert_id();
    }

2 个答案:

答案 0 :(得分:2)

您必须使用result_array()或result()函数从db获取值,以便更改模型,如

function getFacilityDetails($facility_code)
    {
        $facility_details = 'facility_details';
        $this->db->where('facility_code', $facility_code);
        return $this->db->get($this->$facility_details)->result();
    }

答案 1 :(得分:1)

link研究CI 3.0。添加了几个新功能。

对于此查询,bs_name不能为null,因此使用反转的逗号给出一些值或在表中指定一些默认值,之后不需要输入 null ,它将自动采用默认值。< / p>

INSERT INTO `merchants` (`bs_name`, `business_no`, `phone_no`, `location`, `licence_no`, `commission`, `balance`, `email`, `username`, `password`) VALUES (NULL, '4', '4', NULL, '4', '4', '4', 'muokid3@gmail.com', '4', 'e4da3b7fbbce2345d7772b0674a318d5').

从query.it中删除 bs_name 将自动按默认值填充。

另一个问题。请确保您的controller name以大写字母

开头

例如。使用example.php

这是CI 3.0.

中的新功能