2017-08-09 22 views
-1

我一直在我的codeigniter微應用程序restful api中得到這個錯誤。當我發佈一個項目時,只有第一個字母被保存,並顯示狀態代碼400。codeigniter restful api在幾個字段中的非法字符串偏移

這裏是我的模型文件:

class Cities_model extends CI_Model { 

    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function save($city) 
    { 
     $this->db->set($this->setCity($city, null))->insert('cities'); 

     if ($this->db->affected_rows() > 0) { 
      return $this->db->insert_id; 
     } 
     return null; 
    } 

    public function update($id, $city) 
    { 
     $this->db->set($this->setCity($city))->where('id')->update('cities'); 
     if ($this->db->affected_rows() === 1) { 
      return true; 
     } 
     return false; 
    } 

    private function setCity($city) 
    { 
     return array(
      'id' => $city['id'], 
      'name' => $city['name'] 
     ); 
    } 
} 
+1

'$這個 - > DB->設置($這個 - > setCity($城市,NULL)) - >插入( '城市'); '將其更改爲'$ this-> db-> insert('cities',array('city'=> $ city));' – jagad89

+0

謝謝,但您尚未包含函數setCity,它是包含要插入。 – vinn

+0

你已經擴展了'CI_Model',所以它沒有任何setter-getter方法,並且實際上將城市值傳遞給函數。 – jagad89

回答

1

正如你可以看到setCity功能治療$city變量數組。所以你需要將數組傳遞給setCity函數。

class Cities_model extends CI_Model { 

    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function save($city) 
    { 
     $this->db->insert('cities',$this->setCity(array('name'=>$cit‌​y,'id'=> null))); 

     if ($this->db->affected_rows() > 0) { 
      return $this->db->insert_id(); 
     } 
     return null; 
    } 

    public function update($id, $city) 
    { 
     $this->db->where('id',$id)->update('cities',$this->setCity(array('name'=>$cit‌​y,'id'=> $id))); 

     if ($this->db->affected_rows() === 1) { 
      return true; 
     } 
     return false; 
    } 

    private function setCity($city) 
    { 
     return array(
      'id' => $city['id'], 
      'name' => $city['name'] 
     ); 
    } 
} 

另一件事是,Codeignitor擁有方法insert_id()知道最後一個插入id。

+0

非常感謝buddy.it的工作。請允許我問你解釋如何使用put來更新數據,因爲它沒有像我期望的那樣工作。 – vinn

+0

我已經更新了代碼。請檢查。 – jagad89

+0

我剛剛得到狀態代碼400沒有更新更新 – vinn

0
<?php 

    defined('BASEPATH') OR exit('No direct script access allowed'); 

    require APPPATH . '/libraries/REST_Controller.php'; 

    class Cities extends REST_Controller{ 

public function __construct() { 
      parent::__construct(); 
      $this->load->model('cities_model'); 
}  
public function index_get(){ 
    $cities=$this->cities_model->get(); 
    if(!is_null($cities)) 
    { 
    $this->response(array('response'=>$cities),200); 
    } 
    else 
    { 
    $this->response(array('error'=>'cities cannot be found...'),404); 
    } 

    } 

    public function find_get($id){ 
    if(!$id) 
    { 
    $this->respose(null,400); 
    } 
    $cit=$this->cities_model->get($id); 
    if(!is_null($cit)) 
    { 
    $this->response(array('response'=> $cit),200); 
    } 
    else{ 
    $this->response(array('error'=> 'data could not be found...'),404); 

    } 
    } 

    public function index_post(){ 

// Use validation library, instead of checking just for value. 
$this->load->library('form_validation'); 
$this->form_validation->set_rules('city','City','trim|required'); 

if($this->form_validation->run() == FALSE) 
{ 
    // send back list of validation errors. 
    $this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST); 
} 

$id=$this->cities_model->save($this->post('city')); 
if(!is_null($id)) 
{ 
    $this->response(array('response'=> $id),REST_Controller::HTTP_OK); 
} 
else 
{ 
    $this->response(array('error'=> 'sorry, data could not be saved...'),REST_Controller::HTTP_BAD_REQUEST); 
} 
} 
public function index_put(){ 
// for put you need to pass id as parameter 

// Use validation library, instead of checking just for value. 
$this->load->library('form_validation'); 
$this->form_validation->set_rules('id','ID','trim|required|integer'); 
$this->form_validation->set_rules('city','City','trim|required'); 

if($this->form_validation->run() == FALSE) 
{ 
    // send back list of validation errors. 
    $this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST); 
} 

$update=$this->cities_model->update($this->post('id'),$this->post('city')); 
if(!is_null($update)) 
{ 
    $this->response(array('response' => 'content updated successfully'),REST_Controller::HTTP_OK); 
} 
else 
{ 
    $this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), REST_Controller::HTTP_BAD_REQUEST); 
    } 
    } 
    public function index_delete($id){ 
    if(!$id) 
    { 
    $this->response(null,400); 
    } 
    $del=$this->cities_model->delete($id); 
    if(!is_null($del)) 
    { 
    $this->response(array('response'=> 'item successfully deleted'),200); 

    } 
    else{ 
    $this->response(array('error'=> 'delete operations could not be done...'),400); 
    } 
    } 
    } 

這裏是模型文件:

<?php 
    class Cities_model extends CI_Model 
{ 
public function __construct() 
{ 
    parent::__construct(); 

} 
public function get($id=null) 
{ 
if(!is_null($id)) 
{ 
    $query=$this->db->select('*')->from('cities')->where('id',$id)->get(); 
    if($query->num_rows()===1) 
    { 
    return $query->row_array(); 
    } 
    return null; 
    } 
    $sql=$this->db->select('*')->from('cities')->get(); 
    if($sql->num_rows()>0) 
    { 
    return $sql->result_array(); 
    } 
    return null; 
    } 
    public function save($city) 
{ 
$this->db->insert('cities', array('name'=>$city)); 
if($this->db->affected_rows()>0) 
{ 
return $this->db->insert_id(); 
} 
return null; 
} 
public function update($id, $city) 
{ 
    $this->db->where('id',$id)->update('cities',$this->setCity(array('name'=>$cit‌​y,'id'=> $id))); 

    if ($this->db->affected_rows() === 1) { 
     return true; 
    } 
    return false; 
} 
private function setCity($city) 
{ 
return array('id'=>$city['id'], 
     'name'=>$city['name'] 
     ); 
} 
public function delete($id) 
{ 
$this->db->where('id',$id)->delete('cities'); 
if($this->db->affected_rows()===1) 
    { 
return true; 
} 
    return false; 
    } 
    } 
+0

那就是整個控制器 – vinn

+0

我現在已經加了完整的代碼 – vinn

相關問題