Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php yii中的FindBySql返回空值_Php_Yii - Fatal编程技术网

Php yii中的FindBySql返回空值

Php yii中的FindBySql返回空值,php,yii,Php,Yii,我是yii的新手。我面临findBySql方法的一个问题。当我试图通过传递Mysql查询和参数来获取记录时,它会返回一个空值 我的代码如下所示。。 在模型中,我定义了一个函数getCountry()来获取国家名称。 class StateMaster extends CActiveRecord { public function tableName() { return 'T_State_Master'; } public function g

我是yii的新手。我面临findBySql方法的一个问题。当我试图通过传递Mysql查询和参数来获取记录时,它会返回一个空值

我的代码如下所示。。 在模型中,我定义了一个函数getCountry()来获取国家名称。

class StateMaster extends CActiveRecord
{
    public function tableName()
    {
        return 'T_State_Master';
    }


    public function getCountry($c_id)
    {       
        //return array(StateMaster::model()->findBySql("select C_Name from T_Country_Master where C_Id=:CountryId;",array(':CountryId'=>$c_id)));       
        $result = array(StateMaster::model()->findBysql("select C_Name from T_Country_Master where C_Id={$c_id}"));
        return $result;
    }
    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return StateMaster the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}
<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(        
        array(
        'label'=>'State Name',      
        'value'=>$model->S_Name,            
        ),
        array(
        'label'=>'Country Name',        
        'value'=>$model->getCountry($model->C_Id),          
        ),      
        array(
        'label'=>'Created Date',        
        'value'=>Yii::app()->dateFormatter->format("dd-MMM-yyyy", $model->CreatedDt),           
        ),
        array(
        'label'=>'Created By',      
        'value'=>$model->CreatedBy,         
        ),      
    ),
)); ?>
然后在我的视图文件中,尝试通过提供国家Id来获取国家名称。

class StateMaster extends CActiveRecord
{
    public function tableName()
    {
        return 'T_State_Master';
    }


    public function getCountry($c_id)
    {       
        //return array(StateMaster::model()->findBySql("select C_Name from T_Country_Master where C_Id=:CountryId;",array(':CountryId'=>$c_id)));       
        $result = array(StateMaster::model()->findBysql("select C_Name from T_Country_Master where C_Id={$c_id}"));
        return $result;
    }
    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return StateMaster the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}
<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(        
        array(
        'label'=>'State Name',      
        'value'=>$model->S_Name,            
        ),
        array(
        'label'=>'Country Name',        
        'value'=>$model->getCountry($model->C_Id),          
        ),      
        array(
        'label'=>'Created Date',        
        'value'=>Yii::app()->dateFormatter->format("dd-MMM-yyyy", $model->CreatedDt),           
        ),
        array(
        'label'=>'Created By',      
        'value'=>$model->CreatedBy,         
        ),      
    ),
)); ?>

我是否想知道为什么它没有给我结果。 我已检查传入的参数及其成功传递。 请给我一个解决方案。
提前感谢

将您的功能更改为:

public function getCountry($c_id)
{
    $query = "select C_Name from T_Country_Master where C_Id={$c_id}";
    //return Yii::app()->db->createCommand($query)->queryAll(); // returns an array, so in your detail view, you must handle it first
    return Yii::app()->db->createCommand($query)->queryScalar();
}

试试这个方法,但如果我是你,我会用第一个

public function getCountry($c_id)
{
    $query = "select C_Name from T_Country_Master where C_Id={$c_id}";
    return Yii::app()->db->createCommand($query)->queryScalar();
}

OR

public function getCountry($c_id)
    {       
        $criteria = new CDbCriteria;  
        $criteria->select="C_Name";
        $criteria->addCondition('C_Id = $c_id');
        $result = StateMaster::model()->find($criteria);
        return $result;
    }

是的,它对我有用。非常感谢。但是你能告诉我如何使用FindBySql()方法吗?那是为了查找多个活动记录,在这里,你只需要一个记录单元(我想),如果C_Id是主键,那么使用$result=Country_Master::model()->findbyPK($C_Id);