我一直试图从过去两个小时的查询中得到结果,在我的模型中我有这个
public function getQuotes()
{
$data = Yii::app()->db->createCommand('Select fromm from city_fare_final');
$data->queryRow();
return $data ;
}
Run Code Online (Sandbox Code Playgroud)
在控制器中
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));
}
}
Run Code Online (Sandbox Code Playgroud)
并在视图中
<div id="moduleResult">
<span><?php echo $model->getQuotes() ;?><------ Here</span>
</div>
Run Code Online (Sandbox Code Playgroud)
但它总是给我一个错误说"类CDbCommand的对象无法转换为字符串",我该怎么做才能得到我的查询结果在模型中???
关心加布里埃尔
public function getQuotes()
{
$data = Yii::app()->db->createCommand('Select fromm from city_fare_final');
$data->queryRow();
return $data ;
}
Run Code Online (Sandbox Code Playgroud)
你的getQuotes()返回Object of class CDbCommand:
+ You returned $data in the function instead of $data->queryRow().
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你不能echo用于array数据.下面的例子用于通过使用带有Yii的DAO从DB获取数据来查看:我想你有Person模型和Person控制器
在您的Person模型中:
function getData() {
$sql = "SELECT * from Person";
$data = Yii::app()->db
->createCommand($sql)
->queryAll();
return $data;
}
Run Code Online (Sandbox Code Playgroud)
在你的控制器中:
function index(){
$data = Person::model()->getData();
$this->render('your_view',array(
'data'=>$data,
));
}
Run Code Online (Sandbox Code Playgroud)
在您的视图中:您可以使用foreach数据来回显数据中的项目array:
<?php foreach($data as $row): ?>
//show something you want
<?php echo $row->name; ?>
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24648 次 |
| 最近记录: |