Search code examples
phpparse-platform

Attach Rows To Relation in Parser using PHP


Hello i have problem attaching relation of my new London Metro Station objects to existing London Metro Line objects. As u can see in the pictures bellow i can easily add relation by clicking it. All i wanna do is make it automatically using PHP. I've put objectId's to this column "Line" on 1st image data types like string, array, but it requires "relation" which i have no idea how to add to. Parse Docs aren't clear in that kind of situation. Please help. ObjectMetroLine Class ObjectMetro Class Attaching Rows to Relation by Click

$json = file_get_contents('https://api.tfl.gov.uk/line/mode/tube/status');
$data = json_decode($json,true);
$x=0;
foreach ($data as $datax){
    $linia = $datax['id'];
    $link = 'https://api.tfl.gov.uk/Line/'.$linia.'/StopPoints';
    $json2 = file_get_contents($link);
    $data2 = json_decode($json2,true);
    foreach ($data2 as $datax2){
      echo 'Nazwa stacji:' .$datax2['commonName'] ."<br>";
        echo 'Lat:' . $datax2['lat']."<br>";
        echo 'Lon:' . $datax2['lon']."<br>";
        $querych = new ParseQuery("ObjectMetro");
        $querych->equalTo("stationId", $datax2['id']);
        $res= $querych->first();
      if(count($res)==0){//Sprawdza czy jest już obiekt o takiej nazwie
              $query = new ParseObject("ObjectMetro");
        $query->set("name", $datax2['commonName']);
        $query->set("longitude", $datax2['lon']);
        $query->set("latitude", $datax2['lat']);
        $query->set("stationId", $datax2['id']);
            $ktora = count($datax2['modes']);
            for($i=0; $i<$ktora; $i++){
                if($datax2['lineModeGroups'][$i]['modeName']==='tube'){
                    $x=$i;
                }
            }
            $ile = count($datax2['lineModeGroups'][$x]['lineIdentifier']);
            for($i=0; $i<$ile; $i++){
                $jaka = $datax2['lineModeGroups'][$x]['lineIdentifier'][$i];
                $querya = new ParseQuery("ObjectMetroLine");
                $querya->equalTo('lineId', $jaka);
                $result = $querya->first();
                $idlinii = $result->getObjectId();
                $query->set("lines", $idlinii);//here problem
                echo 'Linia: '. $jaka . ' ' . $idlinii . '<br>';
            }
          try{
          $query->save();
            echo 'New object created with objectId: ' . $query->getObjectId();
        }catch (ParseException $ex) {  
          echo 'Failed to create new object, with error message: ' . $ex->getMessage();
            }
            echo '<hr>';
        }
 }
};


Solution

  • You can use this guide as a reference: https://docs.parseplatform.org/php/guide/#many-to-many-relationships

    That's what you should do:

    $json = file_get_contents('https://api.tfl.gov.uk/line/mode/tube/status');
    $data = json_decode($json,true);
    $x=0;
    foreach ($data as $datax){
        $linia = $datax['id'];
        $link = 'https://api.tfl.gov.uk/Line/'.$linia.'/StopPoints';
        $json2 = file_get_contents($link);
        $data2 = json_decode($json2,true);
        foreach ($data2 as $datax2){
          echo 'Nazwa stacji:' .$datax2['commonName'] ."<br>";
            echo 'Lat:' . $datax2['lat']."<br>";
            echo 'Lon:' . $datax2['lon']."<br>";
            $querych = new ParseQuery("ObjectMetro");
            $querych->equalTo("stationId", $datax2['id']);
            $res= $querych->first();
          if(count($res)==0){//Sprawdza czy jest już obiekt o takiej nazwie
                  $query = new ParseObject("ObjectMetro");
            $query->set("name", $datax2['commonName']);
            $query->set("longitude", $datax2['lon']);
            $query->set("latitude", $datax2['lat']);
            $query->set("stationId", $datax2['id']);
            $relation = $query->getRelation("lines");
                $ktora = count($datax2['modes']);
                for($i=0; $i<$ktora; $i++){
                    if($datax2['lineModeGroups'][$i]['modeName']==='tube'){
                        $x=$i;
                    }
                }
                $ile = count($datax2['lineModeGroups'][$x]['lineIdentifier']);
                for($i=0; $i<$ile; $i++){
                    $jaka = $datax2['lineModeGroups'][$x]['lineIdentifier'][$i];
                    $querya = new ParseQuery("ObjectMetroLine");
                    $querya->equalTo('lineId', $jaka);
                    $result = $querya->first();
                    $relation->add($result);
                    echo 'Linia: '. $jaka . ' ' . $idlinii . '<br>';
                }
              try{
              $query->save();
                echo 'New object created with objectId: ' . $query->getObjectId();
            }catch (ParseException $ex) {  
              echo 'Failed to create new object, with error message: ' . $ex->getMessage();
                }
                echo '<hr>';
            }
     }
    };