[SOLVED] Many_Many relation and listbox?

Hi everyone,

I have 2 Models: Users and AdPositions and a linking table user_has_ad_position. With this I can check where each user may put his ads.

Now the problem is with the assignment of these permissions. When I update a user I want to see a list with his current positions (these should be selected) PLUS all the other ones that I might assign (unselected).

So basically this is what I have:

/models/User.php




    public function relations() {

        return array(

            'positions' => array(self::MANY_MANY, 'AdPosition', 'user_has_ad_position(userid, ad_positionid)'),

        );


    public function getAdpositions() {

        // this gives all possible ad_positions

        $listData = CHtml::listData(AdPosition::model()->findAll(), 'ad_positionid', 'label');


        /*

        // this gives just the assigned ad_positions

        $listData = CHtml::listData($this->positions, 'ad_positionid', 'label');

        */


        return $listData;

    }



And in views/user/_form.php:




...

<?php echo CHtml::activeListBox($model, 'adpositions',$model->getAdpositions()); ?>

...



There might also be a problem with the field ‘adpositions’. Because in the User model I obviously don’t have any attribute for AdPositions other than my relation.

Thanks a lot for your help

Solution (thanks to yjeroen in IRC):

[code]

public function getAdpositions() {


    // this gives all possible ad_positions





    return CHtml::listData(AdPosition::model()-&gt;findAll(), 'ad_positionid', 'label');


}





public function getAssignedAdpositions() {


    return &#036;this-&gt;positions;


}

[/php]

and in the view:




<?php echo CHtml::activeListBox($model, 'assignedAdpositions',$model->getAdpositions(), array('multiple'=>'multiple')); ?>



Also assignedAdpositions isn’t really an attribute it behaves like one thanks to the magic __get() method.

how did you solve it?