Yii2 get data from many to many relation in gridview and apply filter

942
March 02, 2017, at 04:11 AM

i'm developing a web application with Yii2 framework, and i'm facing a problem right now. I want to display the data from a many-to-many relation in a gridview and be able to filter from those fields later on.

I've read the official documentation here, some stackoverflow post like this and other resources but can't seem to get it to work. I have 3 tables: actividad, plan_actividad and circulo_icare, actividad is related to plan_actividad and circulo_icare is also related to it (plan_actividad is the junction table). So i have defined the following relations in my Actividad model:

class Actividad extends \yii\db\ActiveRecord 
{
   .... 
public function getPlanActividad()
{
    return $this->hasMany(PlanActividad::classname(), ['act_id' => 'act_id']);
}
public function getCirculo()
{
    return $this->hasMany(CirculoIcare::classname(), ['cirica_id' => 'act_id'])->via('planActividad');
}
...
}

The in my view index.php i'm trying to show the values in a gridview like this:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    // 'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        // 'act_id',
        ['attribute' => 'Codigo Evento', 'value' => 'act_numorden'],
        ['attribute' => 'Nombre Evento', 'value' => 'act_nombre'],
        ['attribute' => 'Fecha Evento', 'value' => 'act_fecha'],
        ['attribute' => 'Locacion', 'value' => 'locacion.loc_nombre'],
        [
        'attribute' => 'Circulo',
        'value' => 'circulo.cirica_nombre',
        ],
        ['attribute' => 'Circulo id',
         'value' => 'planActividad.cirica_id',
        ],
        // 'act_horaini',
        // 'act_horafin',
        // 'act_idencuesta',
        // 'act_vigencia:boolean',
        // 'loc_id',
        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

The problem is, i can't get any values to show with the circulo relation, it always shows (not set). If i change hasMany in getPlanActividad() with hasOne() then it shows some values (only 2 of 11 it should, based on the cirica_id that exist on plan_actividad table) but these are not correct anyway. I know that i can filter for those fields later on in search view but i don't really understand why the relations doesn't work as i expected.

Any help would be greatly appreciated, let me know if more info is needed and thank you in advance.

Rent Charter Buses Company
READ ALSO
how to access only one object send by json not the whole array in ajax success function?

how to access only one object send by json not the whole array in ajax success function?

Hi guys i am trying to receive data from controller to view using json_encode and ajax function but i am sending to separate values through json and tring to access it so how can i separate those two objects and get only a single valuelet me show you my code

337
PHP shopping cart order that is emailed displays only last product ordered and not every ordered product

PHP shopping cart order that is emailed displays only last product ordered and not every ordered product

I am playing around with a PHP shopping cart and I am trying to send the order of the customer via email to the seller

368
Generating PHP server stubs with thrift

Generating PHP server stubs with thrift

So yesterday i wrote my own thrift IDL which went fine and now I'm trying to generate some code from it With that in mind I went to generate code in PHP, Java, C# and Python with my first written IDL So on thrift 010

351
Rename SonataUserBundle roles

Rename SonataUserBundle roles

I use SonataUserBundle extending FOSUserBundle, So I need to rename roles name because I have that in my group form :

305