Has_Many Relations On Class Search


My class ‘Escritorio’, have a relation hasMany.

  • @property Empresa[] $empresas

Empresa has a column escritorio_id <---- foreign key for escritorio

In the class EscritorioSearch how do i include this relation into search() method?

And show it on GridView?

Thanks everyone

There’s some information here which might help.

If it works the same as Yii 1.1.x, you’ll need to group on the ID field of the primary table to prevent the row counts from being incorrect when joining a has many relation. I’d be interested to know if this is still the case.

Good Afternoon,

In my columns on gridView, setting the property like this:


            'attribute' => 'empresas',

            'value' => 'empresas.nome',


*empresas = companies

*empresas.nome = companies name / Attribute Name.

this is my HasMany relation…

office -> HasMany -> Companies

[size=“5”]But in the gridView, the columns show the message: ‘Not Defined’.

[b]If i like to search in gridview this works!

Add this lines to modelSearch to work the search columns by companie name.

        $query = Escritorio::find();

        $subQuery = Empresa::find()



        $query->leftJoin(['empresas' => $subQuery], 'empresas.escritorio_id = psw_escritorio.id');


I created a test enterprise and i searched for it.

See attachments:

1 - All Registers

2 - Registers Filtered

What is wrong to not show the company name in gridview? :(

Best Regards



I resolved the problem.

Created a function, this returned all companies separated by comma