I am trying to retrieve data from database using query builder, store the result in CArrayDataProvider and display it in view file. but i am getting this error:
Kindly check whether the following is helpful in our case.
CONTROLLER
public function actionIndex()
{
$model=new Search;
if(isset($_POST['Search']))
{
$searchresult = Yii::app()->db->createCommand()
->select('p.product_id, p.product_name, r.price, p.master_key')
->from('tbl_product_index p')
->join('tbl_pricing r', 'p.product_id=r.product_id')
->where('master_key=:master_key', array(':master_key'=>$_POST['Search']['brand']['1']))
->queryAll();//fetching all rows...
$dataProvider=new CArrayDataProvider($searchresult);
$this->render('view',
array(
'dataProvider'=>$dataProvider,//pass $dataProvider not searchresult.
)
);
}
$this->render('index',array('model'=>$model,));
}
VIEW
$dataArray = $dataProvider->getData();
foreach ($dataArray as $data){
echo CHtml::encode($data["product_id"]);//Here $data represents associative array with column names as index representing a row
}
Kindly check whether the following is helpful in our case.
CONTROLLER
public function actionIndex()
{
$model=new Search;
if(isset($_POST['Search']))
{
$searchresult = Yii::app()->db->createCommand()
->select('p.product_id, p.product_name, r.price, p.master_key')
->from('tbl_product_index p')
->join('tbl_pricing r', 'p.product_id=r.product_id')
->where('master_key=:master_key', array(':master_key'=>$_POST['Search']['brand']['1']))
->queryAll();//fetching all rows...
$dataProvider=new CArrayDataProvider($searchresult);
$this->render('view',
array(
'dataProvider'=>$dataProvider,//pass $dataProvider not searchresult.
)
);
}
$this->render('index',array('model'=>$model,));
}
Thanks Seenivasan,this was helpful.
however now i am facing issue in creating command. the query provides an output if i remove the part which involves related tables (like r. price and Join sentence) but it gives empty result if using the below code.
if(isset($_POST['ProductIndex']))
{
$searchresult = Yii::app()->db->createCommand()
->select('p.product_id, p.product_name, r.price, p.master_key') //works fine without r.price
->from('tbl_product_index p')
->join('tbl_pricing r', 'r.product_id=p.product_id') //works fine without this sentence
->where('master_key=:master_key', array(':master_key'=>$_POST['ProductIndex']['brand']['1']))
->queryAll();
$dataProvider=new CArrayDataProvider($searchresult);