SQLDataprovider starts at 0

Hello

I’m writing a simple Yii2 app

I have problem with my sqldataprovider starting count from 0 which creates troubles accesing the correct data later in Gridview. Dataprovider work fine, though i need to retrieve data from 2 tables under a new name and its easier for me to do such thing with good old sql.

My dataProvider code




        $count = (int)Towar::find()->count();

        $dataProvider = new SqlDataProvider([

            'sql' => 'SELECT towar.nazwa,cena,dostep,promo,producent,kategorie.nazwa as kategoria FROM towar,kategorie WHERE `id_kat`=kategorie.id',

            'totalCount' => $count,

            'pagination' => [

                'pageSize' => 40,

            ],           

        ]);

Ok now I’m totally lost

I converted it somehow to ActiveDataProvider like this


   $query = new Query();

        $dataProvider = new ActiveDataProvider([

            'query' => $query->select(['towar.nazwa','cena','dostep','promo','producent','kategorie.nazwa as kategoria'])->from(['towar'])->leftJoin('kategorie','id_kat=kategorie.id'),


            'pagination' => [

                'pageSize' => 40,

            ],

        ]);

but again first record gives me id=0 which is incorrect

This approach gives me correct id that starts at 1 but I cant get data from 2 different models like this I think


$query = new Query();

        $dataProvider = new ActiveDataProvider([

            'query' => Towar::find(),

            'pagination' => [

                'pageSize' => 40,

            ],

        ]);

The yii2 grid with ActiveDataProvider approach is that you define the relationships between the models inside the models themselves so you don’t have to write join queries as you have done. In the gridview you can just have direct access to the related models.

eg in your gridview widget you can do something like




	<?= GridView::widget([

		'dataProvider' => $dataProvider,

		'filterModel' => $searchModel,

		'columns' => [


			[

				'attribute' => 'kategorie.nazwa',

				'header'    => 'Kategorie',

			],



Emm and how the DataProvider should look in this case ? if I use Towar::find() it wont allow me to retrieve attribute from Kategorie and it seems perfectly logical.

All i want is to retrieve every record from Towar and display it in gridview using corresponding names from Kategorie . Queries i written does this perfectly only with this 1 small issue that they generate links in gridview that starts with id=0 instead of id=1. My models are gii generated and have 1:1 relation defined on Towar.id_kat=Kategorie.id

//why this forum thinks it is a spam when i quote your post oO