在过去的两个小时内,我一直在尝试从查询中获取结果,在我的模型中,我有这个
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 的对象无法转换为字符串",我该怎么做才能获得在模型中进行的查询结果???
问候加布里埃尔
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
数据。下面的示例用于使用 DAO 和 Yii 从数据库获取数据以供查看: 我想你有 人物模型 和 人控制器
在"人员"模型中:
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; ?>
$data->queryRow();
以数组格式返回结果。您的代码返回$data
这是一个对象,而不是查询的结果。这就是您收到此错误的原因。
如果要获取单个值,可以使用$data->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
}
我希望这会有所帮助。
下面的示例代码遍历 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"] ;
}
返回包含字段的行的行的排列
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;
}
控制者:通知控制者.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,
)
);