-1

i am working on a cakephp 2.x ... well the scenario is that i am sending messages to my webapp ... there are almost 509 messages ... the problem is it is saving some messages into db and on some messages it gives me an error on android console ... ...so first thing i want to ask what u all think that where the actual problem lies or can be lie..and the other think is there a way that i can throw some exception error on data which is not saving into the db .. so i can track the particular messages which is not saving into and causing the problem ...i need some help in debugging this code

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){

            $date = $datas['date'];
            $mobileNo = $datas['mobileNo'];
            $mobileNo = AllSecure::replaceDashesAndSpaces($mobileNo);


            $body = $datas['body'];
            $timestamp = $date/1000;
            $date = date('Y-m-d h:i' , $timestamp);

            $this->request->data['Message']['mobileNo'] = $mobileNo;

            $this->request->data['Message']['body'] = $datas['body'];
            $this->request->data['Message']['type'] = $datas['type'];
            $this->request->data['Message']['User_id'] = $datas['idUser'];
            $this->request->data['Message']['dateTime'] = $date;

            $count = $this->Message->checkTextMessages($mobileNo,$body,$date,$datas['idUser']);
            if($mobileNo!=null){
                if($count>0){
                }else{
                    $this->Message->create();
                    $this->Message->save($this->request->data


                }
            }

        }
    }


}
hellosheikh
  • 2,929
  • 8
  • 49
  • 115
  • Haven't tested your code but if your are saving data from within a loop use `saveAll` instead of `save`. – Konsole Jul 24 '13 at 15:12
  • You can check the return value of the `save` method. If it's false, there was a failure and you can throw your exception. – dhofstet Jul 24 '13 at 15:18
  • @ankit thankyou ok i will try may be it'll do something – hellosheikh Jul 24 '13 at 15:19
  • Apart from looking quite odd (the code updates `$this->request->data` in a loop, instead of a variable) - there's nothing obviously wrong with the code in the question. `it gives me an error on android console` - what error? Also, what version of Cake are you using - you've tagged two different versions. – AD7six Jul 24 '13 at 15:20
  • @dhofstet how ? i mean which exception i throw ? or i should hard cord it like e.g echo "error" ? – hellosheikh Jul 24 '13 at 15:20
  • @AD7six well AD whenever an error comes on my web app .. it always gives the same thing on android that auth component bla bla .. this time too displaying the same .. and on the end saying that line 42 appcontroller ... which i think has nothing to do with this code... so i want to ask is that possible if something speciall character may be is comming on the body and it is not saving ? – hellosheikh Jul 24 '13 at 15:24
  • Oh I see, then all you need to do is `bla bla`. Thanks for providing the error message which permitted this specific answer :). – AD7six Jul 24 '13 at 15:24
  • well if you are asking then i can show you the error message ... – hellosheikh Jul 24 '13 at 15:25
  • @hellosheikh It's up to you what you do in that situation, whether you throw an exception, use an echo statement, or whatever. – dhofstet Jul 24 '13 at 15:46

1 Answers1

0

well i solved my problem by self ... i dont know why it is causing the problem ... so i did this .. and it works

  foreach($data as $datas){
      $i=0
       $this->request->data['Message'][$i]['mobileNo'] = $datas['mobileNo'];;

            $this->request->data['Message'][$i]['body'] = $datas['body'];
            $this->request->data['Message'][$i]['type'] = $datas['type'];
            $this->request->data['Message'][$i]['User_id'] = $datas['idUser'];

            $i++;

            if($mobileNo!=null){

            }

        }

        $isSave = $this->Message->saveAll($this->request->data["Message"]);
        echo $isSave;
hellosheikh
  • 2,929
  • 8
  • 49
  • 115