【问题标题】:CakePHP 2.5 Datasource, create and return responseCakePHP 2.5 数据源,创建并返回响应
【发布时间】:2014-12-01 19:49:10
【问题描述】:

我有一个特定的任务,将 CakePHP Web 应用程序连接到远程 restful 服务器。我创建了一个数据源,读取方法效果很好,但是保存数据后的 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; // ??????
    }

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

【问题讨论】:

    标签: cakephp cakephp-2.0 datasource


    【解决方案1】:

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

    数据源

    ....
    
    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;
        }
    .....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 2021-09-28
      • 2013-04-13
      • 2012-08-18
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多