使用 PHP 将行附加到解析器中的关系

Attach Rows To Relation in Parser using PHP

您好,我在将我的新伦敦地铁站对象与现有伦敦地铁线对象的关系关联时遇到问题。正如您在下面的图片中看到的那样,我可以通过单击轻松添加关系。我想做的就是使用 PHP 自动生成它。我已经将 objectId 放在第一个图像数据类型(如字符串、数组)的“行”列中,但它需要“关系”,我不知道如何添加。在这种情况下,Parse Docs 并不清楚。请帮忙。 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>';
        }
 }
};

您可以将本指南用作参考:https://docs.parseplatform.org/php/guide/#many-to-many-relationships

那是你应该做的:

$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>';
        }
 }
};