CakePHP 2.5数据源,创建并返回响应

CakePHP 2.5数据源,创建并返回响应,cakephp,cakephp-2.0,datasource,Cakephp,Cakephp 2.0,Datasource,我有一个特定的任务将CakePHP web应用程序连接到远程restful服务器。我创建了一个datasource,read方法非常有效,但是保存数据后的api返回一个处理过的数据数组 正在寻找返回数据数组并在控制器中使用的方法 我的控制器代码 public function admin_generate() { $data = $this->request->data; $data['path'] = 'special/generate'; $this

我有一个特定的任务将CakePHP web应用程序连接到远程restful服务器。我创建了一个datasource,read方法非常有效,但是保存数据后的api返回一个处理过的数据数组

正在寻找返回数据数组并在控制器中使用的方法

我的控制器代码

public function admin_generate()
{
    $data = $this->request->data;   
    $data['path'] = 'special/generate';
    $this->Tool->create();
    if($this->Tool->save($data)){
      // handle response ????
    }
    $this->set('data',$data);
    $this->set('_serialize','data');

}
    public function create(Model $model, $fields = null, $values = null)
    {
    $data = array_combine($fields, $values);
    $api = $this->config['api_path'].$data['path'].'?auth_key='.$this->config['auth_key'];

    $json = $this->Http->post($api, $data);

    $response = json_decode($json, true);
    if (is_null($response)) {
        $error = json_last_error();
        throw new CakeException($error);
    }
    return $response; // ??????
    }
....

if (is_null($response)) {
    $error = json_last_error();
    throw new CakeException($error);
}
// SOLUTION
$model -> code = $response['code'];
$model -> key = $response['key'];
$model -> code_id = $response['code_id'];
return true;
.....
在数据源文件中

public function admin_generate()
{
    $data = $this->request->data;   
    $data['path'] = 'special/generate';
    $this->Tool->create();
    if($this->Tool->save($data)){
      // handle response ????
    }
    $this->set('data',$data);
    $this->set('_serialize','data');

}
    public function create(Model $model, $fields = null, $values = null)
    {
    $data = array_combine($fields, $values);
    $api = $this->config['api_path'].$data['path'].'?auth_key='.$this->config['auth_key'];

    $json = $this->Http->post($api, $data);

    $response = json_decode($json, true);
    if (is_null($response)) {
        $error = json_last_error();
        throw new CakeException($error);
    }
    return $response; // ??????
    }
....

if (is_null($response)) {
    $error = json_last_error();
    throw new CakeException($error);
}
// SOLUTION
$model -> code = $response['code'];
$model -> key = $response['key'];
$model -> code_id = $response['code_id'];
return true;
.....

有人能告诉我在控制器中使用api响应数据的正确方法吗?

在发帖几分钟后,我找到了一个解决方案。这可以帮助你们中的一个

数据源

public function admin_generate()
{
    $data = $this->request->data;   
    $data['path'] = 'special/generate';
    $this->Tool->create();
    if($this->Tool->save($data)){
      // handle response ????
    }
    $this->set('data',$data);
    $this->set('_serialize','data');

}
    public function create(Model $model, $fields = null, $values = null)
    {
    $data = array_combine($fields, $values);
    $api = $this->config['api_path'].$data['path'].'?auth_key='.$this->config['auth_key'];

    $json = $this->Http->post($api, $data);

    $response = json_decode($json, true);
    if (is_null($response)) {
        $error = json_last_error();
        throw new CakeException($error);
    }
    return $response; // ??????
    }
....

if (is_null($response)) {
    $error = json_last_error();
    throw new CakeException($error);
}
// SOLUTION
$model -> code = $response['code'];
$model -> key = $response['key'];
$model -> code_id = $response['code_id'];
return true;
.....
在控制器中

.....
if($this->Tool->save($data)){
        unset($data['path']);
        $data['code'] = $this->Tool->code;
        $data['key'] = $this->Tool->key;
        $data['code_id'] = $this->Tool->code_id;
    }
.....