Many to Many with input field

I just started to work with Yii and I have some problem to figure the way to do it.

I have 3 tables (categories, languages and categories_language) the last one being a pivot. In this pivot table I have

CatId : integer

LangId : Integer

Name : Varchar(50)

Using the crud generator I get checkbox to make the link but I would like also to be able to input the ‘Name’ field at the same time. How can I do that ?

Hope I explained it well.

you mean add a inputfield of the column of other table?

first you need change something in current model, like

class SomeModel extends CActiveRecord


    public $anotherName;


    public function rules()


        return array(


            array('anotherName', 'safe'),




    protected function beforeSave()


        $relation = $this->someRelation;

        $relation->someName = $this->anotherName;

        $relation->validate() && $relation->update();


        return parent::beforeSave();



then you can add a textfield in view like

$af = $this->beginWidget('CActiveForm');

$af->textField($someModel, 'anotherName');

hope useful to you :slight_smile:

No I want to input the column ‘Name’ which is in the pivot table.

This is usually the structure I use for multi-languages site.

The easiest would be to use Gii to generate a model for your ‘categories_language’ table, and then change your category and language models accordingly.

Then you can use Yii magic. ;)

In your Language model:

        	'categories' => array(self::MANY_MANY, 'Category', '{{categories_language}}(CatId, LangId)'),

You can use Gii to generate your Category and Language models.

You will get a diff when you preview.

You don’t need to actually save the models. The diff will show you what to add.

Then it’s just a matter of using some code like this:

foreach($language->categories as $category) {

echo $category->Name . '<br/>';


You should of course use it to build an array for use in your select dropdown box.