1个模型2个查询(CodeIgniter)

时间:2018-07-20 08:05:09

标签: php codeigniter

如何在1个模型中放置2个查询? 这是我的代码

public function declineDec($email,$desc,$prop_id,$fk_table){


        $sql = "INSERT INTO amilyar_applicant_notif (email,notif_fk_table,notif_fk_id,notif_content) VALUES (?,?,?,?)";
        $data = [$email,$fk_table,$prop_id,$desc];
        $query = $this->db->query($sql,$data);
        return $this->db->affected_rows();


        $sql2 ="UPDATE amilyar_properties SET prop_approved = 3 WHERE prop_id = ?";
            $data2 = ($prop_id);
            $this->db->query($sql2, $data2);

        // return $sql;
        return $this->db->affected_rows();

2 个答案:

答案 0 :(得分:0)

尝试进行如下修改:

public function declineDec($email,$desc,$prop_id,$fk_table) {
    $sql = "INSERT INTO amilyar_applicant_notif (email,notif_fk_table,notif_fk_id,notif_content) VALUES (?,?,?,?)";
    $data = [$email,$fk_table,$prop_id,$desc];
    $this->db->query($sql,$data);
    $return['insert'] = $this->db->affected_rows();

    $sql2 ="UPDATE amilyar_properties SET prop_approved = 3 WHERE prop_id = ?";
    $data2 = ($prop_id);
    $this->db->query($sql2, $data2);
    $return['update'] =  $this->db->affected_rows();

    return $return;
}

如果要执行更多代码步骤,则无需在return中使用return $this->db->affected_rows();

答案 1 :(得分:0)

希望这对您有帮助:

您的查询应该是这样的,并且您应该将事务与支持事务安全表类型的数据库一起使用

public function declineDec($email,$desc,$prop_id,$fk_table)
{

    $sql = "INSERT INTO amilyar_applicant_notif (email,notif_fk_table,notif_fk_id,notif_content) VALUES (?,?,?,?)";
    $data = [$email,$fk_table,$prop_id,$desc];

    $sql2 = "UPDATE amilyar_properties SET prop_approved = 3 WHERE prop_id = ?";
    $data2 = array($prop_id);


    $this->db->trans_start();

    $this->db->query($sql,$data);
    $this->db->query($sql2, $data2);

    $this->db->trans_complete();
    if($this->db->trans_status())
    {
       return TRUE;
    }
    else
    {
        return FALSE;  
    }
}

更多信息:https://www.codeigniter.com/user_guide/database/transactions.html