Difference between #11 and #12 of
How to create/update a model with its related items using Listbox or CheckboxList

Revision #12 has been created by softark on Mar 24, 2018, 12:38:30 AM with the memo:

fixed typo in a sample code
« previous (#11)

Changes

Title unchanged

How to create/update a model with its related items using Listbox or CheckboxList

Category unchanged

Tutorials

Yii version changed

2.0

Tags unchanged

listBox checkboxList

Content changed

[...]
### PostWithCategories model

And the last one is extended from Post. It has an additional attribute called 'category_ids' that will be used to handle the categories of the post. Note that **'category_ids' attribute is an array** of category ids.


```php

class PostWithCategories extends Post
{
[...]
We want to add a small utility method to Category model.


```php

class Category extends ActiveRecord
{
[...]
{
$categories = self::find()->order('name')->asArray()->all();
$items = ArrayHelper::map($categor
yies, 'id', 'name');
return $items;
}
[...]
### Create action


```php

/**
* Create Post with its categories
[...]
In the view script, we can use a listBox with multiple selection or a checkboxList to select the categories.


```php

<?php $form = ActiveForm::begin([
'id' => 'post-form',
[...]
It's almost the same with Create action:


```php

/**
* Update a post with its categories
[...]
It's different from create.php only in the text of the submit button:


```php

...
<?= Html::submitButton('Update', [
'class' => 'btn btn-primary'
]) ?>
...
[...]
5 0
6 followers
Viewed: 87 983 times
Version: 2.0
Category: Tutorials
Written by: softark
Last updated by: softark
Created on: Jan 27, 2016
Last updated: 6 years ago
Update Article

Revisions

View all history