2014-07-14 3 views
1

첫 번째 레코드가 계속 표시됩니다. 그래, 내가 $model[0]을 썼다는 것을 알았지 만, 왜 제품 ID가 주어진 첫 번째 레코드를 얻는 함수를 사용하지 않았는가? 매개 변수가 무엇이든 관계없이 첫 번째 레코드 만 반환합니다.기준에 올바른 행을 반환하지 않습니다.

컨트롤러 :

foreach ($basket as $item){  
    if($item->product->store_id==$value){ 
    //here... tried hard coding different things in the params.. doesn't work 
    $shipping = table::model()->getProductShipRate(322, $from, $to);   
    $ship_fee = $shipping[0]->ships_to_fee; 
    //blah blah 
    print_r($shipping[0]->add_cost); // always give me the first entry. 
    } 
    } 

모델 :

public function getProductShipRate($pid,$from,$to){ //doesn't do anthing dont know why. 
      $criteria = new CDbCriteria(); 
      $criteria->condition='product_id="'.$pid.'"'; 
      $criteria->condition='ships_from="'.$from.'"'; 
      $criteria->condition='ships_to="'.$to.'"';  
      $record =$this->findAll($criteria); 

      if(!empty($record)) 
       return $record; 
     } 

답변

2

오류가 여기에 있습니다 :

$criteria = new CDbCriteria(); 
$criteria->condition='product_id="'.$pid.'"'; 
$criteria->condition='ships_from="'.$from.'"'; 
$criteria->condition='ships_to="'.$to.'"';  

옆 문자열로 조건을 교체했다.) (.. PARAMS를 사용하는을위한

public function getProductShipRate($pid,$from,$to){ 
    return $this->findAll(
       'product_id = :pid AND ships_from = :from AND ships_to = :to' 
       ['pid' => $pid, 'from' => $from, 'to' => $to] 
    ); 
} 
+0

+1하거나 $ criteria-> PARAMS = 배열을 사용할 수 있습니다;

$criteria = new CDbCriteria(); $criteria->addCondition('product_id="'.$pid.'"'); $criteria->addCondition('ships_from="'.$from.'"'); $criteria->addCondition('ships_to="'.$to.'"'); 

을하지만 더 나은이 모양이 사용 – NEWBIE

관련 문제