listData using with() and MANY_MANY


I’ve table A and B, joined by a table C with many_many relations.

That is, A has many_many B’s.

In A I’ve defined my relation.

I need to create a listData with optgroups key, which should be something like this

SELECT a.description, b.description, b.code


JOIN b ON (..)

ORDER BY a.description, b.description

I should be able to do this in one shot using findAll and listData, which supports optgroups, but cannot understand how!

I actually solved this way, but I’d like to understand how to do that using listData

        $var1 = A::model()->with('relationName')->findAll();

        foreach ($var1 as $c) {

            foreach ($c->relationName as $h) 

                $ret [$c->codiceDescrizione][$h->codice] = $h->codiceDescrizione;


        return $ret;