OK, this is a small example:

===================================================================
The MySQL Dump of the database:
/*Table structure for table `MenuItemsTesting` */

DROP TABLE IF EXISTS `MenuItemsTesting`;

CREATE TABLE `MenuItemsTesting` (
   `ID` int(10) unsigned NOT NULL auto_increment,
   `ItemText` varchar(50) character set utf8 collate utf8_unicode_ci NOT
NULL,
   `IsTopMenu` tinyint(1) default NULL,
   `HasSubMenu` tinyint(1) default NULL,
   `Link` varchar(255) character set utf8 collate utf8_unicode_ci default
NULL,
   `ParentMenuID` int(10) unsigned default NULL,
   `TopMenuOrder` int(10) unsigned default NULL,
   UNIQUE KEY `ID` (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=120 DEFAULT CHARSET=latin1;

/*Data for the table `MenuItemsTesting` */

insert  into
`MenuItemsTesting`(`ID`,`ItemText`,`IsTopMenu`,`HasSubMenu`,`Link`,`Pare\
ntMenuID`,`TopMenuOrder`) values (1,'Menu 1',1,0,'',0,1),(2,'Menu 1 Item
1',0,0,'',1,1),(3,'Menu 1 Item 2',0,0,'',1,2),(4,'Menu
2',1,0,'',0,2),(5,'Menu 2 Item 1',0,0,'',4,1),(6,'Menu 2 Item
2',0,0,'',4,2),(7,'Menu 2 Item 1',0,0,'',4,3),(8,'Menu
3',1,0,'',0,3),(9,'Menu 3 Item 1',0,0,'',8,1),(10,'Menu 3 Submenu
1',0,1,'',8,2),(11,'Menu 3 Submenu 1 Item 1',0,0,'',10,1),(12,'Menu 3
Submenu 1 Item 2',0,0,'',10,2);
===================================================================

On the PHP server I have the following setup for WebORB:

===================================================================
/WebORB/Services/WeborbTesting/VOs/Menu.php:
<?php
class Menu
{
     public $ID;
     public $label;
     public $ParentMenuID;
     public $children;

     function __construct()
     {
         $this->label = "";
         $this->ParentMenuID = null;
         $this->children = array();
     }
}
?>
===================================================================
/WebORB/Services/WeborbTesting/VOs/MenuItem.php:
<?php
class MenuItem
{
     public $ID;
     public $label;
     public $Link;
     public $ParentMenuID;

     function __construct()
     {
         $this->label = "";
         $this->Link = "";
         $this->ParentMenuID = null;
     }
}
?>
===================================================================
/WebORB/Services/WeborbTesting/MenusService.php:
<?php
require_once "VOs/MenuItem.php";
require_once "VOs/Menu.php";

class MenusService
{
     /**
      * Get Menus
      * @returns An Array of Menus
      */
     function getMenus()
     {
         $menus = array();
         $menuDB = new mysqli("myserver.com", "user", "password",
"database");
         if(!mysqli_connect_errno())
         {
             $query = "SELECT * FROM MenuItemsTesting WHERE IsTopMenu=1
ORDER BY TopMenuOrder ASC";
             $queryResult = $menuDB->query($query);
             $menusArray = array();
             if($queryResult->num_rows > 0)
             {
                 while ($row = $queryResult->fetch_assoc())
                 {
                     $menu = new Menu();
                     $menu->ID = (int)$row['ID'];
                     $menu->label = $row['ItemText'];
                     $menusArray[] = $menu;
                 }
             }

             foreach ($menusArray as $menu)
             {
                 $menu = (object)$menu;
                 $query = "select * from MenuItemsTesting WHERE
ParentMenuID=" . $menu->ID . " ORDER BY TopMenuOrder";
                 $queryResult = $menuDB->query($query);
                 if($queryResult->num_rows > 0)
                 {
                     $menuItems = array();
                     while ($row = $queryResult->fetch_assoc())
                     {
                         if($row['HasSubMenu'])
                         {
                             $menuItem = new Menu();
                             $menuItem->ID = (int)$row['ID'];
                             $menuItem->label = $row['ItemText'];
                             $menuItem->ParentMenuID =
$row['ParentMenuID'];
                             $menuItem->children =
$this->getSubMenu($row['ID']);
                         }
                         else // Not a Sub Menu
                         {
                             $menuItem = new MenuItem();
                             $menuItem->ID = (int)$row['ID'];
                             $menuItem->label = $row['ItemText'];
                             $menuItem->ParentMenuID =
$row['ParentMenuID'];
                             $menuItem->Link = $row['Link'];
                         }
                         $menuItems[] = $menuItem;
                     }
                     $menu->children = $menuItems;
                 }
                 $menus[] = $menu;
             }
         }
         return $menus;
     } // End Function getMenus

     function getSubMenu($parentMenuID)
     {
         $subMenuItemsArray = array();
         $subMenuDB = new mysqli("myserver.com", "user", "password",
"database");
         $subMenuQuery = "select * from MenuItemsTesting WHERE
ParentMenuID=" . $parentMenuID . " ORDER BY TopMenuOrder";
         $subMenuQueryResult = $subMenuDB->query($subMenuQuery);
         if($subMenuQueryResult->num_rows > 0)
         {
             while($subMenuRow = $subMenuQueryResult->fetch_assoc())
             {
                 if($subMenuRow['HasSubMenu'])
                 {
                     $subMenuItem = new Menu();
                     $subMenuItem->ID = (int)$subMenuRow['ID'];
                     $subMenuItem->label = $subMenuRow['ItemText'];
                     $subMenuItem->ParentMenuID =
$subMenuRow['ParentMenuID'];
                     $subMenuItem->children =
$this->getSubMenu($subMenuRow['ID']);
                 }
                 else
                 {
                     $subMenuItem = new MenuItem();
                     $subMenuItem->ID = (int)$subMenuRow['ID'];
                     $subMenuItem->label = $subMenuRow['ItemText'];
                     $subMenuItem->ParentMenuID =
$subMenuRow['ParentMenuID'];
                     $subMenuItem->Link = $subMenuRow['Link'];
                 }
                 $subMenuItemsArray[] = $subMenuItem;
             }
         }
         return $subMenuItemsArray;
     }
}
?>
===================================================================

Now for the Flex side...

I have a myservices folder with Menu.as and MenuItem.as files. I also
have a customcomponents folder with MyMenuBar.mxml file:

===================================================================
myservices/Menu.as:
package myservices
{
     [RemoteClass(alias="WeborbTesting.VOs.Menu")]
     [Bindable]
     public class Menu
     {
         //instance variables
         private var _ID:String;
         private var _label:String;
         private var _ParentMenuID:int;
         private var _children:Array = new Array();

         //accessor methods
         public function get ID():String {return _ID;}
         public function get label():String {return _label;}
         public function get ParentMenuID():int {return _ParentMenuID;}
         public function get children():Array {return _children;}

         //mutator methods
         public function set ID(ID:String):void {_ID = ID;}
         public function set label(label:String):void {_label = label;}
         public function set ParentMenuID(ParentMenuID:int):void
{_ParentMenuID = ParentMenuID;}
         public function set children(children:Array):void {_children =
children;}
     } // end class Menu
}//end package
===================================================================
myservices/MenuItem.as:
package myservices
{
     [RemoteClass(alias="WeborbTesting.VOs.MenuItem")]
     [Bindable]
     public class MenuItem
     {
         //instance variables
         private var _ID:String;
         private var _label:String;
         private var _Link:String;
         private var _ParentMenuID:int;

         //accessor methods
         public function get ID():String {return _ID;}
         public function get label():String {return _label;}
         public function get Link():String {return _Link;}
         public function get ParentMenuID():int {return _ParentMenuID;}

         //mutator methods
         public function set ID(ID:String):void {_ID = ID;}
         public function set label(label:String):void {_label = label;}
         public function set Link(Link:String):void {_Link = Link;}
         public function set ParentMenuID(ParentMenuID:int):void
{_ParentMenuID = ParentMenuID;}
     } // end class MenuItem
}//end package
===================================================================
customcomponents/MyMenuBar.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Box width="100%" xmlns:mx="http://www.adobe.com/2006/mxml";
creationComplete="onCreationComplete();">
     <mx:Script>
         <![CDATA[
             import mx.rpc.events.FaultEvent;
             import mx.rpc.events.ResultEvent;
             import mx.rpc.remoting.RemoteObject;
             import mx.messaging.channels.AMFChannel;
             import mx.messaging.ChannelSet;
             import mx.controls.Alert;
             import mx.collections.*;
             import mx.events.MenuEvent;
             import mx.managers.CursorManager;
             import mx.utils.ArrayUtil;
             import myservices.Menu;
             import myservices.MenuItem;

             private var menuService:RemoteObject;
             private var channelSet:ChannelSet;
             private var amfChannel:AMFChannel;

             [Bindable] private var menuBarCollection:ArrayCollection;

             private function onCreationComplete():void
             {
                 channelSet = new ChannelSet();
                 amfChannel = new AMFChannel("my-amf",
"http://myserver.com/WebORB/weborb.php";);
                 channelSet.addChannel(amfChannel);
                 menuService = new RemoteObject();
                 menuService.channelSet = channelSet;
                 menuService.destination = "WeborbTesting.MenusService";
                 menuService.requestTimeout = 30;
                
menuService.getMenus.addEventListener(ResultEvent.RESULT,
menuDataResult);
                 menuService.addEventListener(FaultEvent.FAULT,
faultHandler);

                 menuService.getMenus();
                 CursorManager.setBusyCursor();
             }

             // Function to get menu data from the server
             private function menuDataResult(event:ResultEvent):void
             {
                 CursorManager.removeBusyCursor();
                 menuBarCollection = new
ArrayCollection(ArrayUtil.toArray(event.result));
             }

             // Event handler for the MenuBar control's itemClick event.
             private function menuHandler(event:MenuEvent):void
             {
                 //navigateToURL(new
URLRequest(MenuItem(event.item).Link), '_self');
                 Alert.show((event.item as MenuItem).Link);
             }

             private function faultHandler(fault:FaultEvent):void
             {
                 CursorManager.removeBusyCursor();
                 switch(fault.fault.faultCode.toString())
                 {
                     case "Client.Error.RequestTimeout":
                         menuService.disconnect();
                          Alert.show("The server is not responding.
Please check that you are connected and the server is running.", "Server
Timeout");
                        break;
                     default:
                         Alert.show(fault.fault.faultString,
fault.fault.faultCode.toString());
                     break;
                 }
             }
          ]]>
     </mx:Script>
     <mx:MenuBar id="menuBar" itemClick="menuHandler(event)"
dataProvider="{menuBarCollection}" />
</mx:Box>
===================================================================

And, finally, the application ;-}

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" xmlns:customcomponents="customcomponents.*">
     <customcomponents:MyMenuBar/>
</mx:Application>

===================================================================

My menubar is a lot more complex as each user gets a different menu bar
depending on his/her access level so I have more columns in my database
as well as more logic in the PHP service and the Flex code but this is a
boiled down example that works.

Note that you don't need to modify your compiler settings nor do you
need a remoting-config.xml file nor a services-config.xml file either in
Flex or within Weborb. The example works as it stands. Of course, you
can add these files. Just remember that the server side and client side
need the same files (It took me a while, at first, to realize this  - I
had the local XML files but had not updated the server side XML files
with my configs and it did not work).

To see the hierarchical nature of the typed objects, fire up Charles and
see what is returned from WebORB.

I hope this helps you (And anybody else working with WebORB). Let me
know if I can be of further assistance.


Best Regards




Steve



--- In [email protected], "Amy" <[EMAIL PROTECTED]> wrote:
>
> --- In [email protected], "valdhor" stevedepp@ wrote:
> >
> > Not with AMFPHP, but I do with WebORB. They should be fairly close.
> >
> > I use it to create my menubar. I have Menu objects and MenuItem
> > objects. Each Menu object can contain MenuItem objects as well as
Menu
> > objects. With this structure, the Flex menubar can take the entire
> > arraycollection as a dataprovider and automatically create my
menubar.
> >
> > Let me know if you would like me to create a small example.
>
> I'd love to see it if you don't mind :-)
>
> Thanks;
>
> Amy
>

Reply via email to