0

i am working on a Cakephp 2.x .. i am sending data from my android app to my Cakephp web app through HTTP Post and then saving into the database..

here is my code

     public function message(){
       $this->loadModel('Message');

    if ($this->request->isPost()){

        $json = $this->request->data('json');

        $data = json_decode($json, TRUE);    

        foreach($data as $datas){

            $mobileNo = $datas['mobileNo'];
            $body = $datas['body'];

            $type = $datas['type'];
            $userId = $datas['idUser'];

            $this->request->data['Message']['mobileNo'] = $mobileNo;
            $this->request->data['Message']['body'] = $body;
            $this->request->data['Message']['type'] = $type;
            $this->request->data['Message']['User_id'] = $userId;
            $this->request->data['Message']['dateTime'] = null;
            $this->Message->save($this->request->data);
        }
    }
 }

i am getting data successfully because when i print out the data

       $mobileNo = $datas['mobileNo'];

it is successfully printing the number ... but dont know why it is throwing me errors on my android app and not saving the data into the database ... i think the problem is related to the Model 'Message'

4

1 回答 1

2

You are missing to call $this->Message->create(); before the save because you're calling save() in a loop. See http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-create-array-data-array

Also check your validation rules and if your android app fails, well, do you send a proper success or error status back to the android app?

Best would be to put the data processing into a model method and unit test that method.

于 2013-07-02T12:09:58.447 回答