如何在插入查询中添加外键值 - mysql - CodeIgniter

时间:2012-12-27 19:56:35

标签: mysql codeigniter foreign-keys

我有以下插入查询:

$salarystuff = array('salary' => $salary, 'from_date' => $salary_from_date, 'to_date' => $salary_to_date);

        $this->db->insert('salaries', $salarystuff);

薪水表有列:emp_no |薪水| FROM_DATE | to_date(我正在使用dev.mysql.com提供的数据库。

但它给了我一个错误1452说外键约束。如何引用另一个表中的键值才能插入此表?

这是错误消息:

  

无法添加或更新子行:外键约束失败   (employeessalaries,CONSTRAINT salaries_ibfk_1 FOREIGN KEY   (emp_no)参考employeesemp_no)ON DELETE CASCADE)

     

INSERT INTO salariessalaryfrom_dateto_date)价值观   ('1000000','2012-12-27','2013-01-16')

谢谢

编辑:我正在尝试以下

首先,我使用此功能在employees表中创建记录:

function add_emp($firstname,$lastname,$gender,$date_of_birth,$jobtitle,$dept,$hiredate)
    {

        $data = array(  'first_name' => $firstname, 
                        'last_name' => $lastname,
                        'gender' => $gender,
                        'birth_date' => $date_of_birth,
                        'hire_date' => $hiredate);

        $this->db->trans_start();
        $this->db->insert('employees', $data);
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE)
        {
            $msg = "Adding the new employee failed.";
            return $msg;
        }
        else
        {
            $msg = "Successfully Added Employee.";
            return $msg;
        }
    }

然后在另一个函数中添加薪水:

function add_salary($firstname,$lastname,$gender,$date_of_birth,$jobtitle,$dept,$hiredate,$salary, $salary_from_date,$salary_to_date)
    {
        $this->db->select('emp_no');
        $this->db->from('employees');
        $this->db->where('first_name', $firstname);
        $this->db->where('last_name', $lastname);
        $this->db->where('gender', $gender);
        $this->db->where('hire_date', $hiredate);
        $this->db->where('birth_date', $date_of_birth);
        $this->db->limit(1);
        $selected_employee = $this->db->get();

        $salarystuff = array('emp_no' => $selected_employee, 'salary' => $salary, 'from_date' => $salary_from_date, 'to_date' => $salary_to_date);
        $this->db->insert('salaries', $salarystuff);
    }

两者都在模型中。然后在控制器中我调用两个函数:

$ employee_insert = $ this-> user-> add_emp($ firstname,$ lastname,$ gender,$ date_of_birth,

$jobtitle,$dept, $hiredate);

        $salarythings = $this->user->add_salary($firstname,$lastname,$gender,$date_of_birth,$jobtitle,$dept,$hiredate,$salary, $salary_from_date,$salary_to_date);

但是我收到一个错误,因为emp_no字段中没有值...我认为它可能就行了:

$selected_employee = $this->db->get();

            $salarystuff = array('emp_no' => $selected_employee,

我的错误信息是:

  

类CI_DB_mysql_result的对象无法转换为字符串

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试插入工资表,但您没有指定工资记录的雇员。您需要为emp_no表中的employees列提供值。

ETA:

很高兴您发布了更多代码 - 您应该做的是修改您的add_emp函数,以便它返回已创建的员工的ID(我在此假设emp_no是并且自动 - 数据库中的增量列)。像这样:

function add_emp($firstname,$lastname,$gender,$date_of_birth,$jobtitle,$dept,$hiredate)
{

    $data = array(  'first_name' => $firstname, 
                    'last_name' => $lastname,
                    'gender' => $gender,
                    'birth_date' => $date_of_birth,
                    'hire_date' => $hiredate);

    $this->db->trans_start();
    $this->db->insert('employees', $data);
    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE)
    {
        //$msg = "Adding the new employee failed.";
        //return $msg;
        return -1; // indicates failure
    }
    else
    {
        //$msg = "Successfully Added Employee.";
        //return $msg;
        return $this->db->insert_id();
    }
}

请注意,调用add_emp的代码需要更改一点以将返回值视为整数而不是现在的消息,但现在您拥有该员工的ID,并且您可以在你的add_salary函数,而不是传递所有其他数据,如:

function add_salary($emp_no,$salary, $salary_from_date,$salary_to_date)
{
    $salarystuff = array('emp_no' => $emp_no, 'salary' => $salary, 'from_date' => $salary_from_date, 'to_date' => $salary_to_date);
    $this->db->insert('salaries', $salarystuff);
}

因此,添加员工及其薪水的代码将是这样的:

$emp_no = add_emp([Your parameters]);
if ( $emp_no > 0 ) {
   add_salary($emp_no, [other parameters]);
} else {
   // Show some error message that the employee creation failed.
}

再次编辑:

如果您忽略我上面的建议,那么您收到错误的简单答案是因为$selected_employee是结果集而不是实际的员工编号。您需要像这样更改代码:

$selected_employee = $this->db->get();
$result = $selected_employee->result();
$emp_no = $result[0]->emp_no;

    $salarystuff = array('emp_no' => $emp_no, 'salary' => $salary, 'from_date' => $salary_from_date, 'to_date' => $salary_to_date);
    $this->db->insert('salaries', $salarystuff);