从cdbcommand Yii获取查询结果

时间:2013-11-16 08:18:59

标签: php yii

我一直试图从过去两个小时的查询中得到结果,在我的模型中我有这个

public function getQuotes()
        {            
            $data = Yii::app()->db->createCommand('Select fromm from city_fare_final');            
            $data->queryRow();            
            return $data ;
        }
控制器中的

    public function actionIndex()
{
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
            $model=new QuoteForm();

    if(isset($_POST['QuoteForm']))
    {
                $model->attributes=$_POST['QuoteForm'];

                if ($model->validate())
                {
                    $priceTable=new CityFareFinal;
                    $priceTable->fromm=$model->pickupL;
                    $priceTable->too=$model->dropoffL;                    
                    $priceTable->type_of_car=$model->type;                        
      this->render('result',array('model'=>$priceTable))                        
                }

                }
            else
            {
                $this->render('index',array('model'=>$model));                
            }
}

并在视图中

    <div id="moduleResult">                          
        <span><?php echo $model->getQuotes() ;?><------ Here</span>
    </div>     

但它总是给我一个错误,说“类CDbCommand的对象无法转换为字符串”,我该怎么做才能得到我在模型中查询的结果???

此致 加布里埃尔

4 个答案:

答案 0 :(得分:7)

public function getQuotes()
        {            
            $data = Yii::app()->db->createCommand('Select fromm from city_fare_final');            
            $data->queryRow();            
            return $data ;
        }

你的getQuotes()返回Object of class CDbCommand

+ You returned $data in the function instead of $data->queryRow(). 

顺便说一下,您无法将echo用于array数据。 以下示例用于通过使用带有Yii的DAO从DB获取数据来查看:我假设您有Person模型和Person控制器

在你的人物模型中:

function getData() {
    $sql = "SELECT * from Person";

    $data = Yii::app()->db
        ->createCommand($sql)
        ->queryAll();
    return $data;
}

在您的控制器中:

function index(){
    $data =  Person::model()->getData();

    $this->render('your_view',array(
    'data'=>$data,
    ));
}

在您的视图中:您可以foreach数据回显array数据中的项目:

<?php foreach($data as $row): ?>
     //show something you want 
     <?php echo $row->name; ?>
<?php endforeach; ?>

答案 1 :(得分:4)

$data->queryRow();以数组格式返回结果。您的代码返回$data,这是一个不是查询结果的对象。这就是您收到此错误的原因。

如果您想获取单个值,可以使用$ data-&gt; queryScalar();

如果queryRow()您的代码将

public function getQuotes()
    {            
        $data = Yii::app()->db->createCommand('Select * from city_fare_final');            
        $result = $data->queryRow();            
        return $result ; //this will return result in array format (single row)
    }

对于您编码的单个字段值

public function getQuotes()
    {            
        $data = Yii::app()->db->createCommand('Select xyz from city_fare_final');            
        $result = $data->queryScalar();            
        return $result; //return single value of xyz column
    }

我希望这会有所帮助。

答案 2 :(得分:1)

下面的示例代码,用于遍历queryAll返回的行

$connection = Yii::app()->db;
$command = $connection->createCommand("Select * from table");
$caterow = $command->queryAll(); //executes the SQL statement and returns the all rows

foreach($caterow as $retcat )
{
    echo  $retcat["ColumnName"]  ;
}

返回带字段

的行的Arrary

答案 3 :(得分:1)

 Model: Notices.php:
---------------------------------
public function getNoticesBlog($offset = 0){
    $dataResult =  Yii::app()->db->createCommand()->select('*')->from($this->tableName())
    ->andWhere("delete_flg=:delete_flg",array(':delete_flg'=>0))
    ->andWhere("publish=:publish",array(':publish'=>1))
    ->limit(3)->offset($offset)->order('created_on DESC')->queryAll();
    return $dataResult;
}

Controller:NoticesController.php

$firstNotices   = Notices::model()->getNoticesBlog(0);
$secondNotices  = Notices::model()->getNoticesBlog(3);
$thirdNotices   = Notices::model()->getNoticesBlog(6);

$this->render('Notices',array(
        'firstNotices'=>$firstNotices,
        'secondNotices'=>$secondNotices,
        'thirdNotices'=>$thirdNotices,
        )
    );