Problem with Gridview and "has-many" relationship

Hi,

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'],


            'user_id',

            'product_ean',          //shows the id

            'product_ean.name',     //displays : '(not set)' in view

            [       

                'attribute' => 'Name',

                'value' => function($model) {

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

                

                },

            ],          //displays empty cell

                      

            'stock',

            'time',

            'price',


            ['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 ( http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/ ) but as you can see in my code, accessing data by ‘product.name’ does not work for me…

need help :)

thanks

found the solution by myself. it was a typo:

instead of


'product_ean.name'

i have to use:


'productEan.name'

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:




[['user_id'],'default','value'=>Yii::$app->user->id],



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