Problem with Gridview and "has-many" relationship


im struggling with the gridview and displaying data from other tables. See attached Image for the relationsships in the database (this is my first post, so im not allowed to add a link to an image hoster). I can display the table “user_has_product”, but instead of the ‘product_ean’ as an integer i want to display the ‘name’ from the table ‘product’.

heres my code in the view with some comments:

    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'product_ean',          //shows the id

            '',     //displays : '(not set)' in view


                'attribute' => 'Name',

                'value' => function($model) {

                    return join(', ', yii\helpers\ArrayHelper::map($model->productEan, 'product_ean', 'name'));



            ],          //displays empty cell





            ['class' => 'yii\grid\ActionColumn'],


    ]); ?>

the model has a link to the table ‘product’ by:

    public function getProductEan()


        return $this->hasOne(Product::className(), ['ean' => 'product_ean']);


i found solutions like this ( ) but as you can see in my code, accessing data by ‘’ does not work for me…

need help :)


found the solution by myself. it was a typo:

instead of


i have to use:


Problem #2:

Creating a new entry to the table is not working. but the form stays in the view, no error occurs… all field are filled correct and the data which is sent looks like this: (its ok)


    'product_ean' => '1'

    'stock' => '5'

    'time' => '1 Day'

    'price' => '9.99'


there should be a field ‘user_id’ which i removed by hand from the form view, because i dont want the user to change it.

if added this rule to the model:


to set the id by default to a value from the database.

is it save to store the user id in an hidden form field? this could my help