A form for many to many relation

following is my article model:




<?php


namespace app\models;


use Yii;


/**

 * This is the model class for table "article".

 *

 * @property integer $id

 * @property string $name

 * @property string $content

 * @property string $coverimage

 * @property string $type

 */

class Article extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'article';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['name', 'content', 'type'], 'required'],

            [['name'], 'string', 'max' => 1000],

            [['content'], 'string', 'max' => 10000],

            [['coverimage'], 'string', 'max' => 500],

            [['type'], 'string', 'max' => 100],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'name' => 'Name',

            'content' => 'Content',

            'coverimage' => 'Cover Image',

            'type' => 'Type',

        ];

    }

}



articletocategories:





<?php


namespace app\models;


use Yii;


/**

 * This is the model class for table "articletocategories".

 *

 * @property integer $id

 * @property integer $category_id

 * @property integer $article_id

 *

 * @property Article $article

 * @property Category $category

 */

class Articletocategories extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'articletocategories';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['category_id', 'article_id'], 'required'],

            [['category_id', 'article_id'], 'integer'],

            [['article_id'], 'exist', 'skipOnError' => true, 'targetClass' => Article::className(), 'targetAttribute' => ['article_id' => 'id']],

            [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'category_id' => 'Category ID',

            'article_id' => 'Article ID',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getArticle()

    {

        return $this->hasOne(Article::className(), ['id' => 'article_id']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCategory()

    {

        return $this->hasOne(Category::className(), ['id' => 'category_id']);

    }

}




This is my article _form.php





<?php


use yii\helpers\Url;

use yii\helpers\Html;

use yii\helpers\ArrayHelper;

use yii\widgets\ActiveForm;

use app\models\Category;

use app\models\Articletocategories;


/* @var $this yii\web\View */

/* @var $model app\models\Article */

/* @var $form yii\widgets\ActiveForm */

?>




<script src="<?php echo Url::base().'/ckeditor/ckeditor.js'; ?>"></script>


<div class="article-form">


    <?php $form = ActiveForm::begin([

            'options' => [

                'enctype' => 'multipart/form-data'

             ]

        ]); ?>


    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'content')->textarea(['id' => 'editor1','maxlength' => true]); ?>


    

    <?= $form->field($model, 'type')->dropdownList(['image' => 'Image', 'video' => 'Video'], ['prompt' => 'Select...']) ?>

    


    <?= $form->field($model, 'coverimage')->fileInput() ?>    




    <?php 


		$categories=Category::find()->all();


		$listCategoryData=ArrayHelper::map($categories,'id','name');


		echo $form->field($articletocategories, 'category_id')->checkboxList($listCategoryData, ['multiple' => true]);


	?>

    


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>




<script type="text/javascript">

    CKEDITOR.replace( 'editor1' );

</script>






getting this not getting what i am doing wrong in the code please reply i am new to yii framework

i am trying to display categories from category table in article form as checkbox list

also i want to know how create, update , details will work.

I hope the following wiki may help you.

http://www.yiiframework.com/wiki/836/how-to-create-update-a-model-with-its-related-items-using-listbox-or-checkboxlist/