Thread: Managed Objects
View Single Post
  #1  
Old 05-15-2007, 05:42 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Managed Objects

Example of Usage:
PHP Code:

function onCreated()
  {
  
join("managedobjectcontext");
  
this.entities null;

  
// Setting up Party Variables
  
temp.eparty loadEntity"Party" );
  if ( 
temp.eparty == null )
    {
    
temp.eparty createEntity"Party" );
      
// Setting up variable configurations... { inverseRelationship, Multiple Items?, Delete Rule }
      
temp.eparty.setValueForKey( {nullfalsenull }, "configName" );
      
temp.eparty.setValueForKey( {nullfalsenull }, "configLeader" );
      
temp.eparty.setValueForKey( {"Party"true"Cascade" }, "configRanks" );
      
      
temp.eparty.setValueForKey"Party""customClass" ); // Maybe you have special implementations! This joins class "Party" when creating a new instance!
      // Adding a bind to a key... Members now is Ranks.Members
      
temp.eparty.addValueForKey( {"Members""Ranks.Members"}, "binds" );
      
// There is also dependencies... But those are still a little complicated.
      // They'll be further elaborated in future releases.
      // temp.eparty.addDependency( "Members", "Ranks.Members" ); // It'll be something like that.
    
}

  
temp.erank loadEntity"Rank" );
  if ( 
temp.erank == null )
    {
      
temp.erank createEntity"Rank" );
        
temp.erank.setValueForKey( {nulltruenull}, "configMembers" );
        
temp.erank.setValueForKey( {nullnullnull}, "configRankSyntax" );
        
temp.erank.setValueForKey( {nullnullnull}, "configRankName" );
        
temp.erank.setValueForKey( {nulltruenull}, "configRankRights" );
        
temp.erank.setValueForKey( {"Ranks"falsenull}, "configParty" );
    }

  
// Creating New Party and add two ranks, three members...
  // Please Note that each time this is executed, a new party of the same name is created.
  
temp.mparty temp.eparty.createManagedObject();
    
temp.mparty.setValueForKey"Pluffy's Gang""Name" );
    
temp.mparty.setValueForKey"Pluffy""Leader" );

    
temp.mrank temp.erank.createManagedObject();
      
temp.mrank.setValueForKey"Leader""RankName" );
      
temp.mrank.setValueForKey"King %s""RankSyntax" );
      
temp.mrank.setValueForKey( {"add""remove"}, "RankRights" );
      
temp.mrank.setValueForKeynull"Members" );
      
temp.mrank.addValueForKey"Pluffy""Members" );
      
temp.mrank.addValueForKey"Gerami""Members" );
      
temp.mrank.setValueForKeytemp.mparty"Party" );

    
temp.mrank temp.erank.createManagedObject();
      
temp.mrank.setValueForKey"Humble Servant""RankName" );
      
temp.mrank.setValueForKey"Slave %s""RankSyntax" );
      
temp.mrank.setValueForKeynull"RankRights" );
      
temp.mrank.setValueForKeynull"Members" );
      
temp.mrank.addValueForKey"Novo""Members" );
      
temp.mrank.setValueForKeytemp.mparty"Party" );

  
// Now lets use it!
  // Showing Ranks
  
temp.ranks temp.mparty.valueForKeyPath"Ranks.RankName" );
  echo( 
temp.ranks ); // returns: {"Leader", "Humble Servant"};

  // Showing Members Divided By Rank
  
temp.membersByRank temp.mparty.valueForKeyPath"Members" );
  echo( 
temp.membersByRank ); // returns {{"Pluffy", "Gerami"}, {"Novo"}};

  // Showing all Members
  
temp.allMembers null;
  for ( 
temp.ranktemp.membersByRank )
    
temp.allMembers.addarraytemp.rank );
  echo( 
temp.allMembers ); // returns {"Pluffy", "Gerami", "Novo" };

  
echo("===");
  
// Delete a Rank!
  
for ( temp.ranktemp.mparty.valueForKey"Ranks" ) )
    {
    if ( 
temp.rank.valueForKey("RankName") != "Humble Servant" )
      continue;
    
    
temp.erank.deleteManagedObjecttemp.rank );
    }

  
// Showing Ranks
  
temp.ranks temp.mparty.valueForKeyPath"Ranks.RankName" );
  echo( 
temp.ranks ); // returns: {"Leader"};

  // Showing Members Divided By Rank
  
temp.membersByRank temp.mparty.valueForKey"Members" );
  echo( 
temp.membersByRank ); // returns {{"Pluffy", "Gerami"}};

  // Showing all Members
  
temp.allMembers null;
  for ( 
temp.ranktemp.membersByRank )
    
temp.allMembers.addarraytemp.rank );
  echo( 
temp.allMembers ); // returns {"Pluffy", "Gerami" };
  
  // Please note that thus far, it doesn't support saving / loading.
  // This will be implemented at a later date.
  // In the meantime -- avoid resetting NPC-Server!
  

Requires http://forums.graalonline.com/forums...ad.php?t=73780

For those who are interested in what I was replicating... The original concept comes from...

http://developer.apple.com/documenta...ata/index.html

For the most part, things are very similar. How you set it up, however, isn't. The terminology, if you want further reference, can be found in the link above.

I hope the globals appreciate my links or else they can just remove it: Ignorance is Bliss, afterall.
Attached Files
File Type: zip Archive.zip (4.0 KB, 235 views)

Last edited by Novo; 05-15-2007 at 06:36 PM..
Reply With Quote