没有返回条件yii中正确的行


not returning the correct row in criteria yii

我一直得到第一个记录。是的,我知道我写了$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;
        }

错误提示:

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

您将条件替换为下一个字符串。使用:

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

但最好看看这个:

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]
    );
}