简体   繁体   中英

FindBySql in yii returns null value

I am new to yii. I am facing a proble with findBySql Method. While i am trying to get a record through passing Mysql query and parameter, it returns me a null value.

Here my code looks like this.. In Model i have defined a function getCountry() to get the country name.

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

Then in my view file trying to get the country name by providing the Country Id to it.

<?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,         
        ),      
    ),
)); ?>

whether i wonder why is it not giving me the result. I have checked the parameter passed into it and its successfully passing. Please give me solution. Thanks in advance

change your function to this:

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

Try this way, but if i were you, i will use first one.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM