Newbie - Call To A Member Function Prepare() On A Non-Object

Hello everyone,

I am new to php and mvc so would really appreciate some help.

I used gii to create a customers mvc (based on my customers table). In my customers table, a group of customers

can be grouped together using the cus_group field.

What I want to do is have a link which will take me from the view detail of a single customer to a list of all customers within their group (the same as if you apply a filter in the GridView).

To do this, I have added this html into the gii generated view

<ul class="nav nav-pills">

<li><a href="../web/index.php?r=customers/family&id=1">Family</a></li>


In my CustomersController I have this method,

public function actionFamily($id)


&#036;searchModel = new CustomersSearch;

&#036;sql = 'SELECT * FROM customers WHERE cus_group ='.&#036;id;

&#036;customers = Customers::findBySql(&#036;sql)-&gt;all();


return &#036;this-&gt;render('family', [

     'dataProvider' =&gt; &#036;customers,

     'searchModel' =&gt; &#036;searchModel,



The family.php view is the same code as the gii generated index.php.

The problem I am getting when I click the link is the following

PHP Fatal Error – yii\base\ErrorException

Call to a member function prepare() on a non-object

  1. in C:\wamp\www\basic\vendor\yiisoft\yii2\widgets\BaseListView.php at line 104

    public function init()


     if (&#036;this-&gt;dataProvider === null) {
         throw new InvalidConfigException('The &quot;dataProvider&quot; property must be set.');
     if (&#036;this-&gt;emptyText === null) {
         &#036;this-&gt;emptyText = Yii::t('yii', 'No results found.');

What am I doing wrong?

I am very new to this, if there is tutorial anyone can point me would be really great.



You’re passing array of models to dataProvider property instead of passing data provider as requested.

Thank you samdark. You’re the man!

(Your reply has really guided me …I thought no one was going to answer)

I changed my controller code to use a dataprovider eg

public function actionFamily($id)


        $searchModel = new CustomersSearch;

        $dataProvider = new SqlDataProvider([

            'sql' => 'SELECT * FROM customers WHERE cus_group ='.$id



        return $this->render('index', [

            'dataProvider' => $dataProvider,

            'searchModel' => $searchModel,



and it worked. Its only a start, but as I said i’m new to php and mvc (so learning yii2 is three times as hard).