2017-10-24 3 views
0
$params = [ 
    ':x1' => $locationBox['leftLongitude'], 
    ':y1' => $locationBox['topLatitude'], 
    ':x2' => $locationBox['rightLongitude'], 
    ':y2' => $locationBox['topLatitude'], 
    ':x3' => $locationBox['rightLongitude'], 
    ':y3' => $locationBox['bottomLatitude'], 
    ':x4' => $locationBox['leftLongitude'], 
    ':y4' => $locationBox['bottomLatitude'], 
    ':x5' => $locationBox['leftLongitude'], 
    ':y5' => $locationBox['topLatitude'] 
]; 

$sql = " 
    .... 
    INNER JOIN tag_geo T3 ON (T3.id = T2.tag_id_b AND ST_Covers(ST_GeogFromText('POLYGON((:x1 :y1, :x2 :y2, :x3 :y3, :x4 :y4, :x5 :y5))'), T3.geo_location)); 
"; 

$connection = \Yii::$app->getDb(); 
$command = $connection->createCommand($sql); 
$command->bindValues($params); 
$result = $command->queryAll(); 

내가 오류가 따옴표로 감싸 매개 변수를 평가하는 데 실패 준비), POLYGON 함수를 감싸는 틱을 제거하면이 POLYGON이 작은 따옴표 안에 있어야하므로 매개 변수가 평가되지만 다른 오류가 발생합니다.Yii2는 문을

답변

1

POLYGON 주위에 작은 따옴표가 있기 때문에 다각형 부분은 그대로 db 엔진에 의해 인식됩니다. 문자열은 POLYGON((:x1 :y1, :x2 :y2, :x3 :y3, :x4 :y4, :x5 :y5))입니다. 여전히 사용 물음표 마커를 작동하지 않을 경우 대신 (?를) 물론

<?php 

$sql = " 
    .... 
    INNER JOIN tag_geo T3 ON (T3.id = T2.tag_id_b AND ST_Covers(ST_GeogFromText(:polygon), T3.geo_location)); 
"; 

$params = [ 
    ":poligon" => sprintf(// Output: POLYGON((x1-value y1-value, x2-value y2-value, ...)) 
      "POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))" 
      , $locationBox['leftLongitude'] 
      , $locationBox['topLatitude'] 
      , $locationBox['rightLongitude'] 
      , $locationBox['topLatitude'] 
      , $locationBox['rightLongitude'] 
      , $locationBox['bottomLatitude'] 
      , $locationBox['leftLongitude'] 
      , $locationBox['bottomLatitude'] 
      , $locationBox['leftLongitude'] 
      , $locationBox['topLatitude'] 
    ) 
]; 

//... 

: 그래서 당신은 SQL 문 istead에 하나의 마커 (:polygon)를 구현해야합니다.

이 상황은 LIKE 키워드를 사용하는 SQL 문을 준비하려고 할 때와 비슷합니다. 예 : Syntax of LIKE in PreparedStatement.

+0

작동하지만 작은 따옴표 나 두 따옴표는 제거해야합니다. 'POLYGON ((% s % s, % s % s, % s % s, % s % s, % s % s))' –

+0

테스트를 마쳤습니다! 그렇습니다. –

+0

그래서, 당신이 말한대로입니까? 작은 따옴표를 제거해야합니까? –