当var_dump显示时,无法返回一条记录

时间:2014-06-23 17:36:51

标签: php yii

我无法退回我的模特。但是当我return var_dump($model)时,我可以看到记录。为什么我不能return $model呢?我没有看到任何错误,只是显示一个空白部分。

我在小部件中有这个:

$model = Table::model()->something1();           
 $id =Yii::app()->request->getQuery('id');

if($model==null){
    $model = new Table;
    if (isset($_POST['Table'])){
        $model->attributes = $_POST['Table'];                   
        $model->product_id = $product_id;
        $model->user_id = Yii::app()->user->user_id;
        $model->save();
    }
}else {
    $m = Table::model()->something2($id);
    //return var_dump($m[0]); I just want one record
    //I also tried just getting $model, doesn't work   
    //I just want to get the model if it's there is a record
    return $m;
}

        $this->renderFile(Yii::getPathOfAlias('app.some.path'). '/_form.php',array(
                'model' => $model,
        ));

来自我模特的继承人:

public function something1(){
$uid = Yii::app()->user->user_id;
$id =Yii::app()->request->getQuery('id');

$criteria = new CDbCriteria;
$criteria->compare('user_id',$uid);
$criteria->compare('product_id',$id);
$criteria->limit = 1;

$record = $this->exists($criteria);
return $record;
}

1 个答案:

答案 0 :(得分:0)

您的模型函数something1()返回布尔值而不是记录,因为您正在调用exists()而不是find()

作为替代方案,您也可以按如下方式编写整个函数:

public function something1() {
    return $this->findByAttributes(array('user_id' => Yii::app()->user->id, 
       'product_id' => Yii::app()->request->getQuery('id')));
}