Call To A Member Function Getdata() On A Non-Object

It seems I am having a problem passing the right format to a widget. Might be trivial, but i cannot figure it out.

I am using the code to try to display some data referenced in a model with a foreign key ($model has a one to many foreign key to "referencedTable" data).


    $this->widget('bootstrap.widgets.TbListView',array(      

  	'dataProvider'=> $model->referencedTable,        	

	'itemView'=>'_someview',    

	)

);

The problem is with the data provider and I have a clue why, but I don’t know what is the yii way to approach this.

The current error is:

Fatal error: Call to a member function getData() on a non-object in

framework/zii/widgets/CBaseListView.php on line 108

u need data from model referencedTable?


$data = referencedTable::model()->findAll();


$this->widget('bootstrap.widgets.TbListView',array(      

        'dataProvider'=> $data,               

        'itemView'=>'_someview',    

        )

);



is this u mean?

or like this


$data = new referencedTable;


$this->widget('bootstrap.widgets.TbListView',array(      

        'dataProvider'=> $data,               

        'itemView'=>'_someview',    

        )

);

yes. along those lines, but the problem here is that the referencedTable is a one-to-many relationship from my original table.

Basically it’s something like this:

  • table1 : members table

  • table2: posts table (many for each member)

so in $model i have one record from the table1 (members table, details of one author)

in $model->referencedTable, through the model methods generated by giix i have all posts from table2 (posts table).

and I want to display the posts, in the member view, using a ListView widget.

I know i can do something like


$data = referencedTable::model()->findAll();

and add a condition to get only the posts for the member I am displaying, but since i already have all the information in $model->referenceTable it seems redundant to ask for it again, right?

And many thanks for helping out Rajith!

Actually, i tried this:




$data = Text::model()->findAll("ref_user_id=".$model->id);

$this->widget('zii.widgets.CListView',

	array(

        'dataProvider'=> $data,

        'itemView'=>'_textview',    )

);



and the sql runs just fine but it seems the TbListView expects some other type of data, since it gives me the exact same error. Any clues?

Fatal error: Call to a member function getData() on a non-object in

Ok. So the problem is that it seems that the CListView requires the data provided to be ActiveRecord, not anything else.So when using


$data = new CActiveDataProvider (bla bla);

everything is ok.

Now the question remains: Can i pass a non ActiveDataProvider to the CListView widget? And how?

From the CListView docu:

You are passing ActiveRecord objects instead of a class implementing IDataProvider to CListView dataProvider property.

We don’t give an array of data directly to a CListView. Instead, we give it a data provider which will provide an array of data. That’s how the CListView is designed.

http://www.yiiframework.com/wiki/381/cgridview-clistview-and-cactivedataprovider