[SOLVED] Array to string conversion

Hi all,

The pagination below is working properly for the field names in the table Products; however, when pulling variable data from tables Power or Categories I receive the notice “Array to string conversion”. I’m not sure if the problem exist in the controller or the view. When $products is printed, variable data appears for both Power and Categories. Thanks in advance for pointing me in the right direction.


Controller


 public function actionIndex()

	{

		$query = Products::find()

	        ->where(['brand_id' => 101])

		->joinWith(['power', 'categories']);

		

		 $pagination = new Pagination([

            'defaultPageSize' =>4,

            'totalCount' => $query->count(),

        ]);

	

		$products = $query

	    ->orderBy('product_name')

            ->offset($pagination->offset)

            ->limit($pagination->limit)

            ->all();

		

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

            'products' => $products,

            'pagination' => $pagination,

        ]);

    }


View

      <div class="col-xs-6 col-sm-3"><?= $products['product_name'] ?></div>

      <div class="clearfix visible-xs-block"></div>     

      <div class="col-xs-6 col-sm-3"><?= $products['power'] ?></div>

      <div class="col-xs-6 col-sm-3"><?= $products['category_name'] ?></div>




When I print the data for $products the following power.power_id & power.power show values. Likewise values appear for Categories.


    [_related:yii\db\BaseActiveRecord:private] => Array

                (

                    [power] => Array

                        (

                            [0] => common\models\Power Object

                                (

                                    [_attributes:yii\db\BaseActiveRecord:private] => Array

                                        (

                                            [power_id] => 3

                                            [power] => Electric and Gas


<div class="col-xs-6 col-sm-3"><?= $products['power']['power'] ?></div>

?

Or if you want you can add the magic function __toString in the class Power :




public function __toString()

{

  return $this->power;

}



Thank you Timmy78! In my case the public function added to the power class worked with the adjustment made to the view which is shown below. Thanks again for your time and consideration. btw Is it possible for me to edit the Title of this post to SOLVED, or is that something that a moderator needs to take care of?


public function __toString()

{

  return $this->power;

}


Recommendation:<div class="col-xs-6 col-sm-3"><?= $products['power']['power'] ?></div>


Solved:<div class="col-xs-6 col-sm-3"><?= $products['power'][0] ?></div>

You may vote up solution.

Edit the post and choose ‘Full Editor’ - then you should be able to edit the title.