如何访问POST数据然后在CakePHP中将其删除?

时间:2013-03-23 18:17:37

标签: php android cakephp post request

我正在构建一个连接到我的远程服务器的Android应用程序。我允许用户添加新内容,但在允许用户添加之前,还必须对其进行身份验证。以下是他们添加内容的示例:

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(SharedVariables.TAG_NAME, name));
params.add(new BasicNameValuePair(SharedVariables.TAG_ADDRESS, address));
params.add(new BasicNameValuePair(SharedVariables.TAG_TYPE, type));
params.add(new BasicNameValuePair(SharedVariables.TAG_DESCRIPTION, description));
params.add(new BasicNameValuePair(SharedVariables.TAG_DIFFICULTY, difficulty));
params.add(new BasicNameValuePair(SharedVariables.TAG_TERRAIN, terrain));
params.add(new BasicNameValuePair(SharedVariables.TAG_LONG, Double.toString(currentLng)));
params.add(new BasicNameValuePair(SharedVariables.TAG_LAT, Double.toString(currentLat)));
params.add(new BasicNameValuePair(SharedVariables.TAG_UID, Integer.toString(2)));

//Retrieve hash and id to identify authenticate user
UserAuth auth = new UserAuth(AddSpot.this);
HashMap<String,String> session = auth.getUserDetails();
params.add(new BasicNameValuePair(SharedVariables.TAG_UID, session.get("id")));
params.add(new BasicNameValuePair(SharedVariables.TAG_HASH, session.get("hash")));

正如您所看到的,在第一部分中,我存储了用户自己添加的内容,但是,我还将用户ID和令牌/哈希传递到POST数据中,如下所示。最后部分。我的问题是,我不想用$this->Location->save($this->request->data)将所有内容添加到数据库表中。相反,我希望能够选择用户ID和哈希并删除它,然后将其保存所有其他东西。我怎么能在CakePHP中做到这一点?

这是我在CakePHP中的方法:

function add()
{               
    if ($this->request->is('post'))
    {
        $response = array();
        $this->loadModel('User');
        if($this->User->validateHash($id, $hash))
        {   
            //Do some logic to remove id and hash from request

            $this->Location->create();
            //Save data from POST
            if ($this->Location->save($this->request->data))
            {
                //Successfully saved
                $this->response->statusCode(200);
            } 
            else
            {
                //Error in saving
                $this->response->statusCode(500);
            }
        }
        else 
        {
            //Does their hash match? No, then they are unauthorized to add
            $this->response->statusCode(401);
        }
    }
    $this->set(compact('response'));
    $this->set('_serialize', array('response'));
}

1 个答案:

答案 0 :(得分:2)

此:

unset($this->request->data['YourModel']['id']);
unset($this->request->data['YourModel']['hash']);