Zainya Masood
Zainya Masood

Reputation: 21

My cgridview is showing only one record in yii

i am newbii in yii development and have an issue while displaying data using cgridview as it is showing only first record Motel,Hotel,Roomcategory and halls are models in which PK and FK are passing ... Code is here

    $sponsorm = Motel::model()->find('MotelId=:MotelId', array(':MotelId'=>Yii::app()->user->id));
      $sponsorhotel = Hotel::model()->find('hotelId=:hotelId', array(':hotelId'=>$sponsorm->MotelId));
      $room = Roomcategory::model()->find('hotelId=:hotelId', array(':hotelId'=>$sponsorhotel->hotelId));
      $halls = Halls::model()->find('hotelId=:hotelId', array(':hotelId'=>$sponsorhotel->hotelId));

           echo CHtml::image(Yii::app()->request->baseUrl.'/img/4.png'); 

         $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'businessowner-grid',
        'dataProvider'=>$room->search(),
       //'filter'=>$sponsore,
            'columns'=>array(
            'roomCategoryNames',
            'noOfRooms',
            'price',

                    ),

                ));


$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'businessowner-grid',
    'dataProvider'=>$halls->search(),
   //'filter'=>$sponsore,
        'columns'=>array(
        'Type',
        //'noOfHalls',
        'seats',
        'price',

                ),

            ));

Upvotes: 1

Views: 798

Answers (1)

Mihkel Viilveer
Mihkel Viilveer

Reputation: 432

Right now you are finding only one record because you use find to get one specific record. So you assign this record data to model variables and then you do

$model->search();

This searches from specific table multiple values but you have set the attributes so, that it matches only one record.

To get Motel gridview data use following code:

$sponsorm = $dataProvider=new CActiveDataProvider('Motel', array(
    'criteria'=>array(
        'condition'=>'MotelId = :MotelId',
        'params' => array(':MotelId'=>Yii::app()->user->id)
    ),
));

It is doing the same as search() but the difference is that searching criteria is different. You can use also code below.

$motelModel = new Model();
$motelModel->MotelId = Yii::app()->user->id;

Now there is only one attribute assigned an is setting a criteria to match all rows what has this user id in MotelId field.

$modelModel->search();

Also, I see that you echo something between logic. Please do not do that. Output data only in views and keep the logic out of views. :)

Upvotes: 2

Related Questions