4 Replies Latest reply on Feb 23, 2012 7:08 AM by OneofHis777

    Sending ArrayCollection Using AMF to PHP

    OneofHis777

      So I have been trying to figure this out for a week to no avail...

       

      Goal:


      To be able to send an array collection of 12 records using the Flash Builder php service and then insert those records into the database (which would end up being 12 new rows in the database).

       

      What I Have So Far:

       

      Here is the code from Flash Builder that sets up my ArrayCollection and sends it to the sever.

       

      protected function button_clickHandler(event:MouseEvent):void

                                    {

       

                                              var test:ArrayCollection = new ArrayCollection;

                                              var incomeGoal12:int = int(incomeGoal.text);

                                              var newStudent12:int = int(newStudentGoal.text);

                                              var upgrades12:int = int(upgrades.text);

                                              var thisCycle:int;

                                              var attritionRate:int = 50;

                                              var endingStudentCount:int = (int(currentStudents.text) + int(newStudentGoal.text)) * ( attritionRate /100);

                                              var growthPercentage:int = (endingStudentCount-int(currentStudents.text)) / endingStudentCount;

       

                                              test.addItem({school_id: school_id, month:"January", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*31, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"February", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*28, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"March", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*31, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"April", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*30, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"May", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*31, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"June", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*30, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"July", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*31, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"August", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*31, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"September", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*30, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"October", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*31, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"November", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*30, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                              test.addItem({school_id: school_id, month:"December", goalyear:fmYear.selectedItem,

                                                        month_goal:(incomeGoal12/365)*31, week_goal: incomeGoal12/52, new_students: newStudent12/12, upgrades: upgrades12/12});

       

                                               for (var i:int = 0; i < test.length; i++)

                                              {

                                                        var goalObj:Object = test.getItemAt(i);

                                                        var goalVO:Wp_mamg_goals = new Wp_mamg_goals();

       

                                                        goalVO.school_id=goalObj.school_id;

                                                        goalVO.month = goalObj.month;

                                                        goalVO.goalyear = goalObj.goalyear;

                                                        goalVO.month_goal = goalObj.month_goal;

                                                        goalVO.week_goal = goalObj.week_goal;

                                                        goalVO.new_students = goalObj.new_students;

                                                        goalVO.upgrades = goalObj.upgrades;

                                                        test.setItemAt(goalVO, i);

                                              }                               

       

                                               createMultipleWp_mamg_goalsResult.token = goalsService.createMultipleWp_mamg_goals(test);

                                               goalsService.commit();

                                    }  

       

       

      Here is the code from my php file that should enter the multiple rows into the database.

       

      public function createMultipleWp_mamg_goals($items = array()) {

       

                          foreach($items as $row)

                          {

                          $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (school_id, month, goalyear, month_goal, week_goal, new_students, upgrades) VALUES (?, ?, ?, ?, ?, ?, ?)");

                          $this->throwExceptionOnError();

       

       

                          mysqli_stmt_bind_param($stmt, 'isiddii', $row['school_id'], $row['month'], $row['goalyear'], $row['month_goal'], $row['week_goal'], $row['new_students'], $row['upgrades']);

                          $this->throwExceptionOnError();

                          }

       

       

                          $autoid = mysqli_stmt_insert_id($stmt);

       

       

                          mysqli_stmt_free_result($stmt);

                          mysqli_close($this->connection);

       

       

                          return $autoid;

                }

       

      Here is the pic of the service:

       

      goalserviceSnip.JPG

       

      Here is a pic of the request once the event is called:

       

      goalservicerequestSnip.JPG

       

      Here is the response:

       

      HTTP/1.1 200 OK

      Date: Thu, 16 Feb 2012 04:01:17 GMT

      Server: Apache/2.2.17 (Win32) PHP/5.3.4

      X-Powered-By: PHP/5.3.4

      Content-Length: 3343

      Content-Type: text/html

       

       

      <br /><font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\services\WpmamggoalsService.php on line <i>156</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>704184</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\gateway.php' bgcolor='#eeeeec'>..\gateway.php<b>:</b>0</td></tr><tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0382</td><td bgcolor='#eeeeec' align='right'>2714592</td><td bgcolor='#eeeeec'>Zend_Amf_Server->handle(  )</td><td title='C:\wamp\www\gateway.php' bgcolor='#eeeeec'>..\gateway.php<b>:</b>69</td></tr><tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0543</td><td bgcolor='#eeeeec' align='right'>3078168</td><td bgcolor='#eeeeec'>Zend_Amf_Server->_handle(  )</td><td title='C:\wamp\www\ZendFramework\library\Zend\Amf\Server.php' bgcolor='#eeeeec'>..\Server.php<b>:</b>629</td></tr><tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0590</td><td bgcolor='#eeeeec' align='right'>3271984</td><td bgcolor='#eeeeec'>Zend_Amf_Server->_dispatch(  )</td><td title='C:\wamp\www\ZendFramework\library\Zend\Amf\Server.php' bgcolor='#eeeeec'>..\Server.php<b>:</b>553</td></tr><tr><td bgcolor='#eeeeec' align='center'>5</td><td bgcolor='#eeeeec' align='center'>1.0834</td><td bgcolor='#eeeeec' align='right'>3459912</td><td bgcolor='#eeeeec'>Zend_Server_Reflection_Method->invokeArgs(  )</td><td title='C:\wamp\www\ZendFramework\library\Zend\Amf\Server.php' bgcolor='#eeeeec'>..\Server.php<b>:</b>359</td></tr><tr><td bgcolor='#eeeeec' align='center'>6</td><td bgcolor='#eeeeec' align='center'>1.0834</td><td bgcolor='#eeeeec' align='right'>3460328</td><td bgcolor='#eeeeec'>Zend_Server_Reflection_Function_Abstract->__call(  )</td><td title='C:\wamp\www\ZendFramework\library\Zend\Server\Reflection\Function\Abstract.php' bgcolor='#eeeeec'>..\Abstract.php<b>:</b>0</td></tr><tr><td bgcolor='#eeeeec' align='center'>7</td><td bgcolor='#eeeeec' align='center'>1.0834</td><td bgcolor='#eeeeec' align='right'>3460744</td><td bgcolor='#eeeeec'><a href='http://www.php.net/call_user_func_array' target='_new'>call_user_func_array</a>(  )</td><td title='C:\wamp\www\ZendFramework\library\Zend\Server\Reflection\Function\Abstract.php' bgcolor='#eeeeec'>..\Abstract.php<b>:</b>380</td></tr><tr><td bgcolor='#eeeeec' align='center'>8</td><td bgcolor='#eeeeec' align='center'>1.0834</td><td bgcolor='#eeeeec' align='right'>3461224</td><td bgcolor='#eeeeec'>WpmamggoalsService->createMultipleWp_mamg_goals(  )</td><td title='C:\wamp\www\services\WpmamggoalsService.php' bgcolor='#eeeeec'>..\WpmamggoalsService.php<b>:</b>0</td></tr></table></font>

       

       

      The Problem:

       

      So I am figuring the main problem is I am not sending the right type (meaning "Array") to the server...or I am not handling the data right on server side.

      I have tried a ton of different things nothing has worked.

       

      Any help is greatly appreciated.

      Thanks,

      Anthony