metadata as arrays

hi, I am trying to expose a function that will update metadata on an entity/object. i have initialized this object ($game) in another function and added the metadata:

$game->squares=array("TL"=>0, "TC"=>0, "TR"=>0, "L"=>0, "C"=>0, "R"=>0, "BL"=>0,"BC"=>0,"BR"=>0);

and my function to edit this is:

$player=get_loggedin_userid();
$game->squares[$space]=$player;
return $game->squares[$space];

where $space is of TL, TC, TR.... sent over the network as a parameter. This returns nothing to my client. i have returned $player and $space for debugging purposes and this returns what is expected. Seems as though what is not working is the squares array. I have a feeling the type of $space is not correct.

Any Ideas? Any help would be appreciated, thanks!

  • i know i can simply create metadata for each key in the array:

    $game->TL=0;
    $game->TC=0;
    $game->TR=0;
    .
    .
    .

    And this works:

    $game->$space=$player;

    but being that there are 9 of these, i thought it would just be easier to make one array rather than 9 different metadata...

  • Elgg's metadata mechanism allow for storing arrays - viz ->

                if ($metadata = get_metadata_byname($vars['entity']->guid, $metadataname)) {
                    if (is_array($metadata)) {
                        $value = '';
                        foreach($metadata as $md) {
                            if (!empty($value)) $value .= ', ';
                            $value .= $md->value;
                        }
                    } else {
                        $value = $metadata->value;
                    }
                } else {
                    $value = '';
                }

    The trick is in the "multiple: option when creating metadata.

  • when i use the function get_metadata_byname($vars['entity']->guid, $metadataname), it comes back as false, so never enters the if statement.

    the $vars['entity'] array is used for the view, correct? this is something i am confused about, i am not using a view at all, all my functions need to do is save information from my client and communicate back what is saved/already saved. the view.php file only echoes hello world, same as in the hello world widget tutorial...so $vars array should be empty?

  • never mind. I'm slightly retarded sometimes.

    thanks for your help.

  • another question:

    you use this to retrieve the value stored:

    $value = $metadata->value;

    now if i want to update the value, can't i just use:

    $metadata->value=$value;

    this is not working for me.

  • create_metadata($user->guid, $v, ${$v}, 'text', $user->guid, 2);
    or
    $user->metadataname=<value...>;
    $user->save();

    Read around this kind of code - e.g. in Profile Actions edit.php

  • thats what i have been trying to do...
    maybe if i give you more information:

    user 1 calls function that does this:

    $game= new ElggObject();
    $game->subtype='subtype';
    $game->access_id=2;
    $game->save();
    $game->player1=get_loggedin_userid();
    $game->turn=get_loggedin_userid();
    $game->save();
    return "successful registration";

    user 2 calls function that does this:

    $games=elgg_get_entities(array('type'=>'object','subtype'=>'subtype','limit'=>20));
    foreach($games as $game){
      if($game->player2==null){
        $game->player2=get_loggedin_userid();
        $game->save();
        return "successful registration";}
    }

    At this point, i just want to have player1 and player2 values to be set, with it being player1's turn.

    then user 1 calls this function :

    $games=elgg_get_entities(array('type'=>'object','subtype'=>'subtype','limit'=>20));
    $player=get_loggedin_userid();

    foreach($games as $game){

    if($game->turn!=$player)
       return "not your turn";
    else{
      //the player updates array and changes the turn to be player2
      $game->turn=$game->player2;
      return "successful";}
    }

    this should update $game->turn to be equal to user2's guid, so when user2 calls a similar function it will not return "not your turn"...but this does not work, $game->turn does not change values.

  • Read back over the code examples above carefully. I think your answer is somewhere there....;)

  • $user->metadataname=<value...>;
    $user->save();

    i have used this suggestion here:

    $game->turn=$game->player2;
    $game->save();

    it does not work. $game->turn still has the value of user1's guid.
    not only that, but the documentation says you do not need to save after editing metadata.
    im not sure what you are referring to.